怎样利用node.js快速启一个本地服务

260 阅读1分钟

用到express,child_process两个插件

npm install express // 启服务
npm install child_process // 打开默认浏览器

首先创建一个run.js文件,内容如下

// node启一个本地服务
const express = require('express')
const app = express()
app.use(express.static('./'))
const open = require('child_process')
app.listen(8080, () => {
  open.exec('start http://localhost:8080')
  console.log('打开URL http://localhost:8080')
})

启了一个端口为8080的服务。注:避免已经有8080端口的服务