node js 入门指南

一旦你已经安装了 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 运行程序,访问 http://localhost:3000,你就会看到一个消息,写着“Hello World”。

参考 http://node-js.club

posted @ 2019-05-15 22:24  sakurawinds  阅读(139)  评论(0)    收藏  举报