简体   繁体   English

如何从节点/ Express服务器提供Vuejs前端文件?

[英]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.

相关问题 我想从我的前端 JavaScript 在节点(服务端)中调用 function - I would like to call a function in node(serve end) from my front end JavaScript 如何使前端应用程序访问节点服务器api - How can I make my front end app access my node server apis 如何使用Node / Express服务我的Web应用程序? - How can I serve my web app with Node/Express? 如何使用 Node.js 服务器将我的图像数据从前端保存到 Mongoose 数据库 - How can I save my image data from front end to Mongoose database using Node.js server 如何从Node.js / Express.js中的CDN提供我的根页面? - How can I serve my root page from a CDN in Node.js / Express.js? 如何将查询参数从我的前端服务传递到我的服务器? - How can I pass a query param from my service on the front end to my server? 如何在我的快速服务器上提供 php 文件作为我的主页 - How to serve php files on my express server as my home page 如何构建和服务我的前端和后端 - How to structure and serve my front and back end 尝试从前端加载.tsv文件。 该文件保留在我的服务器上。 如何使用Express做到这一点? - Trying to load a .tsv file from the front end. The file stays on my server. How do I do this using Express? 如何使用 node.js 从服务器向 Angular 前端提供 30GB 的 pdf 文件(静态)? - How to serve 30GB of pdf files(static) from server using node.js to Angular front end?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM