npm home $package
npm repo $package
npm outdated
npm prune
require(`http`)
.createServer()
.listen({
port: 8080,
host: `localhost`,
})
.on(`request`, (req, res) => {
res.end(`Hello World!`);
});
const fs = require(`fs`);
const path = require(`path`);
// why have I always done this...
fs.readFile(path.join(__dirname, `myFile.txt`), (err, data) => {
// do something
});
// when I could just do this?
fs.readFile(`./path/to/myFile.txt`, (err, data) => {
// do something
});
- 别忘了console.dir(obj,{colors:true})能够以不同的色彩打印出键与值,这一点会大大增加日志的可读性。
const dailyCleanup = setInterval(() => {
cleanup();
}, 1000 * 60 * 60 * 24);
dailyCleanup.unref();
const fs = require(`fs`);
// bad
fs.readFile(`./myFile.txt`, `utf8`, (err, data) => {
data.split(`\r\n`).forEach(line => {
// do something
});
});
// good
const os = require(`os`);
fs.readFile(`./myFile.txt`, `utf8`, (err, data) => {
data.split(os.EOL).forEach(line => {
// do something
});
});
node> http.STATUS_CODES
-
1.http.get(url,function(ress){});
http.get(url,function(ress){ var t=''; ress.on('data',function(data) { t+=data; res.write(t); }); ress.on('end',function(data) { res.end(); }); }).on('error',function(e){ console.log(e); });
-
2.http.request(opt,function(res){});
var postData = querystring.stringify({ 'msg' : 'Hello World!' }); var opt={ hostname: 'www.google.com', port: 80, path: '/upload', method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded', 'Content-Length': postData.length } }; var req = http.request(options, (res) => { console.log(`STATUS: ${res.statusCode}`); console.log(`HEADERS: ${JSON.stringify(res.headers)}`); res.setEncoding('utf8'); res.on('data', (chunk) => { console.log(`BODY: ${chunk}`); }); res.on('end', () => { console.log('No more data in response.') }) }); req.on('error', (e) => { console.log(`problem with request: ${e.message}`); }); // write data to request body req.write(postData); req.end();
-
3.app.post(url,function(req,res,next){});
-
4.request.post(url,{json:true,form:params},function(err,response,body){});
- node.js 下使用Express 如何接收这种提交方式.需要 body-parse 插件支持
- 在node.js 里处理这类表单还需要 中间件 connect-multiparty
- bodyParser 支持此类参数解析. 注意: 在提交之前需要指定http 请求头为 content-type=application/json
- 这种请求类型不是特别常见, body-parse默认也不解析这种数据格式.注意:我们还是要使用 body-parse 得到字符串,然后再转化.xml格式请求需要指定 http 请求头 content-type=text/xml.利用req上定义的事件 data 来获取http请求流, end 事件结束请求流的处理.利用 xml2json 把上面得到的请求参数流(我们直接转化为字符串)转化为 json 对象.