使用node.js+express搭建服务器,将vue打包后生成的dist目录在本地运行

8 篇文章 0 订阅


前言

vue项目打包后生成的dist目录如果直接在本地打开index.html,在浏览器中会报错,无法运行起来。

如果想在本地运行,我们可以借助node.js+express搭建一个服务器,将打包后的文件部署在服务器上,就可以运行起来了。


一、nodejs+express搭建服务器

  1. Express 是一个简洁而灵活的 node.js Web应用框架, 提供了一系列强大特性帮助你创建各种 Web 应用,和丰富的 HTTP 工具,使用 Express 可以快速地搭建一个完整功能的网站。
  2. 全局安装express-generator
npm install express --save -g
npm install express-generator --save -g
  1. 安装完成后使用express创建一个项目
    express server (server为项目名称,由你自己定)
    在这里插入图片描述
  2. 创建好项目之后
   cd server 进入项目
   npm install 下载依赖
   npm start  启动项目

5.看到如下提示,说明启动成功
在这里插入图片描述
6.在浏览器输入localhost:3000
在这里插入图片描述

服务器搭建完成

二、在搭建好的服务器中运行本地dist目录

将dist目录中的文件复制到server/public目录下
然后执行 npm start 
在浏览器输入localhost:3000
  • 6
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这是一个简单的关于公益活动的项目,使用 Node.jsVue.js 开发。 后端代码(使用 Express 框架): ```js const express = require("express"); const bodyParser = require("body-parser"); const mongoose = require("mongoose"); const app = express(); app.use(bodyParser.urlencoded({ extended: false })); app.use(bodyParser.json()); mongoose.connect("mongodb://localhost:27017/gongyi", { useNewUrlParser: true, useUnifiedTopology: true }); const activitySchema = new mongoose.Schema({ title: String, description: String, date: Date, location: String, volunteers: [String], donations: Number }); const Activity = mongoose.model("Activity", activitySchema); app.get("/api/activities", async (req, res) => { const activities = await Activity.find(); res.send(activities); }); app.post("/api/activities", async (req, res) => { const activity = new Activity({ title: req.body.title, description: req.body.description, date: req.body.date, location: req.body.location, volunteers: [], donations: 0 }); await activity.save(); res.send(activity); }); app.post("/api/activities/:id/volunteers", async (req, res) => { const activity = await Activity.findById(req.params.id); activity.volunteers.push(req.body.name); await activity.save(); res.send(activity); }); app.post("/api/activities/:id/donate", async (req, res) => { const activity = await Activity.findById(req.params.id); activity.donations += req.body.amount; await activity.save(); res.send(activity); }); app.listen(3000, () => { console.log("Server started on port 3000"); }); ``` 前端代码(使用 Vue.js 框架): ```html <!DOCTYPE html> <html> <head> <title>Gongyi Activity Manager</title> <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> <script src="https://unpkg.com/axios/dist/axios.min.js"></script> </head> <body> <h1>公益活动管理系统</h1> <div id="app"> <h2>添加新活动</h2> <form @submit.prevent="addActivity"> <div> <label>标题:</label> <input type="text" v-model="newActivity.title"> </div> <div> <label>描述:</label> <textarea v-model="newActivity.description"></textarea> </div> <div> <label>日期:</label> <input type="date" v-model="newActivity.date"> </div> <div> <label>地点:</label> <input type="text" v-model="newActivity.location"> </div> <button type="submit">添加</button> </form> <h2>活动列表</h2> <ul> <li v-for="activity in activities"> <h3>{{ activity.title }}</h3> <p>{{ activity.description }}</p> <p>日期:{{ activity.date }}</p> <p>地点:{{ activity.location }}</p> <p>志愿者:{{ activity.volunteers.join(", ") }}</p> <p>捐款:{{ activity.donations }}</p> <form @submit.prevent="addVolunteer(activity)"> <div> <label>姓名:</label> <input type="text" v-model="newVolunteerName"> </div> <button type="submit">报名志愿者</button> </form> <form @submit.prevent="donate(activity)"> <div> <label>金额:</label> <input type="number" v-model="donationAmount"> </div> <button type="submit">捐款</button> </form> </li> </ul> </div> <script> new Vue({ el: "#app", data: { activities: [], newActivity: { title: "", description: "", date: "", location: "" }, newVolunteerName: "", donationAmount: 0 }, methods: { async addActivity() { const response = await axios.post("/api/activities", this.newActivity); this.activities.push(response.data); this.newActivity = { title: "", description: "", date: "", location: "" }; }, async addVolunteer(activity) { const response = await axios.post(`/api/activities/${activity._id}/volunteers`, { name: this.newVolunteerName }); activity.volunteers = response.data.volunteers; this.newVolunteerName = ""; }, async donate(activity) { const response = await axios.post(`/api/activities/${activity._id}/donate`, { amount: this.donationAmount }); activity.donations = response.data.donations; this.donationAmount = 0; } }, async created() { const response = await axios.get("/api/activities"); this.activities = response.data; } }); </script> </body> </html> ``` 这是一个简单的公益活动管理系统,可以添加活动、报名志愿者、捐款等。你可以将代码复制到本地运行,或者根据自己的需求进行修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值