简单的nodejs http-server httpserver
直接安装 http-server :
npm install http-server -g
加参数-g就可以在任何目录启动:
http-server . --port 80
生成证书和密钥文件
openssl req -newkey rsa:2048 -new -nodes -x509 -days 3650 -keyout key.pem -out cert.pem
http-server E:\www -S -C E:\dev\www\cert.pem -K E:\dev\www\key.pem
自己写:
var http = require('http'); http.createServer(function (request, response) { // 发送 HTTP 头部 // HTTP 状态值: 200 : OK // 内容类型: text/plain response.writeHead(200, {'Content-Type': 'text/plain'}); // 发送响应数据 "Hello World" response.end('Hello World\n'); }).listen(8888); // 终端打印如下信息 console.log('Server running at http://127.0.0.1:8888/');
运行:
node main.js
Server running at http://127.0.0.1:8888/
顺别提一下,使用supervisor自动监控文件变化,可以自动重启。
npm i -g supervisor
supervisor test.js
解决 supervisor : 无法加载文件 C:\Users\charles\AppData\Roaming\npm\supervisor.ps1 在使用vsCode中运行cnpm install时报错。 解决方法 1.在win10 系统中搜索框 输入 Windows PowerShell,选择 管理员身份运行2、使用,win+R打开了powershell命令行之后,输入set-ExecutionPolicy RemoteSigned,然后更改权限为A,最后通过 get-ExecutionPolicy 查看当前的状态
可以读取本地文件,从httpserver返回的示例:
var http = require('http'); var fs = require('fs');var server = http.createServer(function (req, res) {
if (req.url === '/serverRedirect') {
res.statusCode = 301;
res.setHeader('Location', 'http://' + req.rawHeaders[1]);
res.end();
} else if (req.url === '/downPdf') {
res.writeHead(200, { 'Content-Type': 'text/plain', 'Content-Disposition': 'attachment;filename=test.pdf' });
res.end('Hello World\n');
} else if (req.url === '/viewPdf') {fs.readFile(</span>'d:/sample.pdf', (err, data) =><span style="color: #000000;"> { console.log(</span>"read success."<span style="color: #000000;">) res.writeHead(</span>200<span style="color: #000000;">, { </span>'Content-Type': 'application/pdf; charset=utf-8'<span style="color: #000000;">, </span>'Content-Disposition': 'inline;filename=test.pdf'<span style="color: #000000;">, </span>"Access-Control-Allow-Origin": "*"<span style="color: #000000;">, </span>"Access-Control-Allow-Headers": "X-Requested-With"<span style="color: #000000;"> }); </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (err) { res.write(</span>'Server error:' +<span style="color: #000000;"> err); } </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> { console.log(</span>"read success."<span style="color: #000000;">) res.write(data); } res.end(); }) } </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> { res.setHeader(</span>'Custom', ['Header'<span style="color: #000000;">]); </span><span style="color: #008000;">//</span><span style="color: #008000;"> 发送 HTTP 头部 // HTTP 状态值: 200 : OK // 内容类型: text/plain </span> res.writeHead(200, { 'Content-Type': 'text/plain'<span style="color: #000000;"> }); let content </span>=<span style="color: #000000;"> req.url; </span><span style="color: #0000ff;">if</span> (req.headers.accept === '*/*;test/header'<span style="color: #000000;">) { content </span>+= 'header/received'<span style="color: #000000;">; } </span><span style="color: #0000ff;">if</span> (req.headers.origin === 'http://new-origin'<span style="color: #000000;">) { content </span>+= 'new/origin'<span style="color: #000000;">; } res.end(content); }
}).listen(8888);
// 终端打印如下信息
console.log('Server running at http://127.0.0.1:8888/');
header上不能加入中文
let nonUtf8Str=Buffer.from([0xcc, 0xec]);
res.setHeader('Content-Disposition', [ Buffer.concat([ Buffer.from(' attachment; filename='),nonUtf8Str]).toString() ]);