怎麼啓動本地服務

方法一: 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會爲它們分不同的端口。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章