天天看點

怎麼啟動本地服務

方法一: Node内置子產品http 的使用

var http = require('http')
http.createServer(function(req, res){
  	res.writeHead(200, { 'Content-Type': 'text-plain' });
  	res.end('Hello World');
}).listen(8083);
           

方法二: 使用 express 的使用

const express = require('express')
const app = express()
// public為靜态資源路徑,預設讀取子檔案index.html
app.use(express.static('public'))
app.listen(8083, () => {
  console.log('server start!')
})
           

方法三: 使用 http-server 也是最友善的方法

// Step1 全局安裝 http-server 
npm install http-server -g 
 
// Step2 進入目标檔案夾啟動 http-server 或者指定端口号 http-server -p 3000 
cd dist  http-server 
cd dist   http-server -p 3000
// Step3 通路 localhost:8080 或者 localhost:3000

進入要啟動的項目所在檔案夾,按住shift+右鍵,選擇 “在此處打開指令視窗”,
出現cmd視窗,輸入指令:hs -o   (等同于  http-server -open)  本地伺服器就啟動起來了,預設端口為8080。
ps: 可以啟動多個項目,http-server會為它們分不同的端口。