启动一个简单http服务器

  |  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
const http = require('http');
const url = require('url');
const util = require('util');

http.createServer((req, res) => {
console.log(req.url);
// res.writeHead(200, { 'Content-Type': 'application/json;charset=utf-8' });
res.writeHead(200, { 'Content-Type': 'text/plain;charset=utf-8' });

// 解析 url 参数
const params = url.parse(req.url, true).query;
res.write('用户名::' + params.name);
res.write('\n');
res.write('城市:' + params.city);
res.end();
// res.end(JSON.stringify({
// 'name': params.name,
// 'city': params.city,
// }));

}).listen(8888);


console.log('Server running at http://127.0.0.1:8888/');

访问
curl http://localhost:8888/?name=klover&city=hz

文章目录