[英]How can I serve my Vuejs front end files from my node/express server?
基本上,我有两条路线,其中一条用于主页,另一条用于管理页面,但是我找不到关于如何组合Vuejs和Express的好的文档,因此我可以在两个页面同时服务的前提下假设两个UI都不同。组件的构造不同。
To serve static files with expressjs
you need to use the static middleware . 要使用expressjs
提供静态文件,您需要使用静态中间件 。
It accepts the directory name as the first argument. 它接受目录名称作为第一个参数。 This directory shall contain all the static files to be served. 该目录应包含要提供的所有静态文件。
const express = require('express');
let app = express();
app.use(express.static('public')); // NAME OF THE DIRECTORY IS PUBLIC
const serverPort = 3000;
const respHttpOptions = {
root: `public/`,
dotfiles: 'deny',
headers: {
'dina-timestamp': Date.now(),
'my-xxx-header': true
}
};
app.get('/', (req, resp) => { // HANDLE THE REQUEST HERE
resp.sendFile('index.html', respHttpOptions, (err) => {
// SEND INDEX.HTML INSIDE PUBLIC DIRECTORY
if (!err)
console.log(sucL(`Served index.html`));
else
console.log(errL(`Failed to serve index.html ${err}`));
})
});
try {
app.listen(serverPort);
console.log(sucL(`Server started at ${serverPort}`));
} catch (e) {
console.log(errL(e));
}
There is no distinction for vuejs! vuejs没有区别! You could have multiple directories inside the static directory. 您可以在静态目录中包含多个目录。
To use vue-router
and avoid running into 404 issues, your express.js
has to have a fallback that serves the index.html. 要使用vue-router
并避免遇到404问题,您的express.js
必须具有为index.html提供服务的后备。 Here is a vue
guide on how to do it https://router.vuejs.org/en/essentials/history-mode.html 这是有关如何执行的vue
指南https://router.vuejs.org/en/essentials/history-mode.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.