将下面代码放入server.js中
JavaScript代码
- var http = require("http");
- http.createServer(function(request, response) {
- console.log('request received');
- response.writeHead(200, {"Content-Type": "text/plain"});
- response.write("Hello World");
- response.end();
- }).listen(8888);
- console.log('server started');
执行node server.js
打开http://localhost:8888/,页面显示Hello World
JavaScript代码
- var http=require('http');
- var data={key:'value', hello:'world'};
- var srv=http.createServer(function (req, res) {
- res.writeHead(200,{'Content-Type': 'application/json'});
- res.end(JSON.stringify(data));
- });
- srv.listen(8080,function() {
- console.log('listening on localhost:8080');
- });