本地启动服务的三种方法

5,803 阅读1分钟

前言

最近学习Nodejs总结出本地启动node服务的几种方式,供大家参考。

方法一: 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!')
})

最后访问localhost:8083 即可访问。

方法三:使用 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