英文标题:How do I start with Node.js after I installed it?(https://nodejs.org/en/docs/guides/getting-started-guide/#how-do-i-start-with-node-js-after-i-installed-it)
当你安装好Node,我们试着去创建我们第一个web服务。先创建一个文件命名为"app.js",粘贴上以下代码:
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
粘贴好后保存,使用 node app.js
命令运行你的web服务,浏览器访问http://localhost:3000,你将会看到“Hello world”字样。