1. 程式人生 > >node js 入門指南

node js 入門指南

-type ctu tor tex UNC erp func ons 消息

一旦你已經安裝了 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

node js 入門指南