1. 程式人生 > >【Node.js】 初體驗

【Node.js】 初體驗

pretty listen 引入 創建 文件 代碼 class 服務 使用

1.新建一個app.js文件

2.使用npm install http 導入http模塊

3.直接上代碼

//引入http模塊
var http = require(‘http‘);
//創建服務器
var app=http.createServer(function (request, response) {

    // 發送 HTTP 頭部 
    // HTTP 狀態值: 200 : OK
    // 內容類型: text/plain
    response.writeHead(200, {‘Content-Type‘: ‘text/plain‘});

    // 發送響應數據 "Hello Node.js"
response.end(‘Hello Node.js‘); })
//添加監聽,監聽端口3000 app.listen(
‘3000‘,()=>{console.info("創建服務器成功,端口號:3000")})

【Node.js】 初體驗