使用node的express库实现静态网站、反向代理和目录浏览
示例1:
var express = require('express');
const { createProxyMiddleware } = require('http-proxy-middleware')
const apiMocker = require('mocker-api')
var serveIndex = require('serve-index')
var app = express()
// app.use(express.static("D:/projects/xxx/"))
app.use(express.static("D:/projects/xxx/", serveIndex(dir, {
view: "detail",
icons: true
}))
app.use("/front", createProxyMiddleware({ target: 'http://localhost:8080/backend', changeOrigin: true }))
apiMocker(app, {
'POST /ss': { "result": "0", "event": [] }
})
app.listen(5000)
示例2:
import express from 'express'
import serveIndex from 'serve-index'
var app = express()
let dir = "D:/www/"
app.use(express.static(dir,), serveIndex(dir, {
view: "detail",
icons: true
}))
app.listen(80)
console.log("listening on 80")
安装为systemd service服务
在目录/etc/systemd/system下,新建文件www80.service
[Unit]
Description=www80
[Service]
WorkingDirectory=/root/serv80
ExecStart=/usr/bin/node /root/serv80/run.mjs
[Install]
WantedBy=multi-user.target
启动服务systemctl start www80
停止服务systemctl stop www80
设置为开机启动systemctl enable www80
浙公网安备 33010602011771号