简体   繁体   English

如何在 Node.js 服务器上部署 Vue.js 应用程序

[英]How to deploy a Vue.js application on Node.js server

I have a dist folder containing CSS, fonts, JS folder and an index.html file minimized for Vue.js, ready to deploy and use.我有一个包含 CSS、字体、JS 文件夹和一个为 Vue.js 最小化的index.html文件的 dist 文件夹,准备部署和使用。 I want to use Node.js to run this application.我想使用 Node.js 来运行这个应用程序。 How can I set this up to just run npm run server and have it deployed on a specific port requested?如何将其设置为仅运行npm run server并将其部署在请求的特定端口上? Not sure how to structure this or if I need to build it in a specific way to run this Vue app.不确定如何构建它,或者我是否需要以特定方式构建它来运行这个 Vue 应用程序。 Any help would be greatly appreciated.任何帮助将不胜感激。

Since Vue is only a frontend library, the easiest way to host it and do things like serve up assets is to create a simple Express friendly script that you can use to start a mini-web server.由于 Vue 只是一个前端库,托管它并执行诸如提供资产之类的事情的最简单方法是创建一个简单的 Express 友好脚本,您可以使用它来启动迷你 Web 服务器。 Read up quickly on Express if you haven't already.如果您还没有阅读Express ,请快速阅读。 After that, add express:之后,添加快递:

npm install express --save

Now add a server.js file to your project's root directory :现在将server.js文件添加到项目的根目录:

// server.js
var express = require('express');
var path = require('path');
var serveStatic = require('serve-static');
app = express();
app.use(serveStatic(__dirname + "/dist"));
var port = process.env.PORT || 5000;
var hostname = '127.0.0.1';

app.listen(port, hostname, () => {
   console.log(`Server running at http://${hostname}:${port}/`);
 });

after that you could run :之后你可以运行:

 node server

and your project will be served at the given host and port并且您的项目将在给定的主机和端口上提供服务

Assuming that you have already the dist directory, if you don't have it run :假设你已经有dist目录,如果你没有运行它:

npm run build

in order to generate it为了生成它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM