var body = ‘Hello World’;
res.setHeader(‘Content-Type’, ‘text/plain’);
res.setHeader(‘Content-Length’, body.length);
res.end(body);
});
ExpressJS框架提供了更高层的方法,比如res.send(),它可以省去诸如添加Content-Length之类的事情。如下:
app.get(‘/hello.txt’, function(req, res){
res.send(‘Hello World’);
});
现在可以绑定和监听端口了,调用app.listen()方法,接收同样的参数,比如:
五、运行程序
现在运行程序,执行命令:
> node app.js
用浏览器访问地址:http://localhost:3000/hello.txt
可以看到输出结果:
Hello World









