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

查看:26
本文介绍了如何在 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 友好脚本,您可以使用它启动一个迷你网络服务器.如果您还没有阅读 Express,请快速阅读.之后,添加快递:

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天全站免登陆