简单的起一个http服务(拖拽文件,外加一行命令)

今天,在群里有个妹子,使用了vue的官方cli去搭建了一个项目,之后打包后想看一下静态资源的路径,然后问我,怎么报错了?

报错代码:

简单的起一个http服务(拖拽文件,外加一行命令)

她的文件配置:

简单的起一个http服务(拖拽文件,外加一行命令)

起初,我以为是路径的的问题,最后和她交流,我问他服务起了吗??才知道她是在本地跑的,其实和大多数人一样,一开始的时候,只是会有很多盲点,会犯错,交流之后,他说有apache服务器,我说也可以的,但是不知道是不是她环境配置问题,还是404,索性,我就给她一个脚本,直接用node 起,废话不多说,直接上代码


建一个js文件name:(prod.server.js)

var express = require('express')
// var config = require('./config/index')
// const proxy = require('http-proxy-middleware'); // 引入代理中间件
// var port = process.env.PORT || config.build.ports

var app = express()

var router = express.Router()
// app.get('/', function (req, res, next) {
// console.log(req.url)

// req.url = '/index.html'
// next()
// })
// app.get('/index', function (req, res, next) {
// req.url = '/index.html'
// next()
// })
// app.get('/loan', function (req, res, next) {
// req.url = '/index.html'
// next()
// })
// router.get('*', function (req, res, next) {
// console.log(req.url)
// req.url = '/index.html'
// next()
// res.render('404', {
// status: 404,
// title: 'NodeBlog'
// })
// next()
// })
let reg = /\.(png|jpe?g|gif|svg|js|css|woff2?|eot|ttf|otf)(\?.*)?$/
router.get('*', function (req, res, next) {
console.log(req.url)
if (!reg.test(req.url)) {
req.url = '/index.html'
}
next()
})
// const apiProxy = proxy('/api', { target: 'https://ifci.lincomb.com', changeOrigin: true }); // 将服务器代理到localhost:8080端口上[本地服务器为localhost:3000]
// app.use('/api/*', apiProxy); // api子目录下的都是用代理
app.use(router)

app.use(express.static('./dist'))
// app.get('*', function(req, res){
// res.render('404.html', {
// title: 'No Found'
// })
// })
module.exports = app.listen(9000, function (err) {
if (err) {
console.log(err)
return
}
console.log('Listening at http://localhost:9000\n')
})



然后直接把这个prod.server.js文件拖到项目根目录下

简单的起一个http服务(拖拽文件,外加一行命令)

加上一个命令行

node prod.server.js

运行界面:

简单的起一个http服务(拖拽文件,外加一行命令)

此时,服务就起起来了,可以复制脚本文件,也可以去我的github上去下载文件