如何在Node.js服务器上部署Vue.js应用程序 [英] How to deploy a Vue.js application on Node.js server

查看:160
本文介绍了如何在Node.js服务器上部署Vue.js应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个dist文件夹,其中包含CSS,字体,JS文件夹和一个针对Vue.js最小化的index.html文件,可以部署和使用.我想使用Node.js运行此应用程序. 如何将其设置为仅运行npm run server并将其部署在请求的特定端口上?不知道如何构造它,或者是否需要以特定方式构建它以运行此Vue应用程序.任何帮助将不胜感激.

I have a dist folder containing CSS, fonts, JS folder and an index.html file minimized for Vue.js, ready to deploy and use. I want to use Node.js to run this application. How can I set this up to just run npm run server and have it deployed on a specific port requested? Not sure how to structure this or if I need to build it in a specific way to run this Vue app. Any help would be greatly appreciated.

推荐答案

由于Vue只是一个前端库,因此托管它并执行诸如提供资产之类的最简单方法是创建一个简单的Express友好脚本,您可以使用该脚本启动小型Web服务器.如果您还没有的话,请在快递上快速阅读.之后,添加快递:

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. Read up quickly on Express if you haven’t already. After that, add express:

npm install express --save

现在将server.js文件添加到项目的根目录:

Now add a server.js file to your project’s root directory :

// 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}/`);
 });

之后,您可以运行:

 node server

,您的项目将在给定的主机和端口上提供

and your project will be served at the given host and port

假设您已经具有dist目录,如果没有运行该目录:

Assuming that you have already the dist directory, if you don't have it run :

npm run build

为了生成它

这篇关于如何在Node.js服务器上部署Vue.js应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆