从零开始用react + tailwindcss + express + mongodb实现一个聊天程序(十四) 部署

修改server下 package.json

{

"name": "server",

"version": "1.0.0",

"description": "",

"main": "index.js",

"scripts": {

"dev": "nodemon src/index.js",

"start": "node src/index.js"

},

"type": "module",

"author": "",

"license": "ISC",

"dependencies": {

"bcryptjs": "^3.0.2",

"chat-app": "file:..",

"cloudinary": "^2.5.1",

"cookie-parser": "^1.4.7",

"cors": "^2.8.5",

"dotenv": "^16.4.7",

"express": "^4.21.2",

"jsonwebtoken": "^9.0.2",

"mongoose": "^8.10.1",

"socket.io": "^4.8.1"

},

"devDependencies": {

"nodemon": "^3.1.9"

}

}

在项目根目录下执行npm init 得到json文件 内容如下

javascript 复制代码
{
    "name": "chat-app",
    "version": "1.0.0",
    "description": "",
    "main": "index.js",
    "scripts": {
      "build": "npm install --prefix server && npm install --prefix web && npm run build --prefix web",
      "start": "npm run start --prefix server"
    },
    "keywords": [],
    "author": "",
    "license": "ISC"
  }

修改server下 index.js

import express from "express"

import authRoutes from './routes/auth.route.js'

import messageRoutes from './routes/message.route.js'

import dotenv from 'dotenv'

import {connectDB} from "./lib/db.js"

import bodyParser from 'body-parser'

import cookieParser from 'cookie-parser'

import cors from 'cors'

import path from "path";

import {app,server} from './lib/socket.js'

dotenv.config();

const PORT = process.env.PORT

const __dirname = path.resolve();

// 跨域配置

// app.options('*', cors()) // include before other routes

app.use(cors({

origin:'http://localhost:5173',

credentials: true

}))

// 解决req.body undefined问题

app.use(cookieParser())

app.use(bodyParser.json())

app.use(bodyParser.urlencoded({ extended: true }))

// 实现signup 注册接口 认证

app.use('/api/auth', authRoutes)

// 消息接口

app.use('/api/messages', messageRoutes)

if (process.env.NODE_ENV === "production") {

app.use(express.static(path.join(__dirname, "../web/dist")));

app.get("*", (req, res) => {

res.sendFile(path.join(__dirname, "../web", "dist", "index.html"));

});

}

server.listen(PORT, () => {

console.log(process.env.NODE_ENV)

console.log("server is running on PORT:" + PORT);

connectDB();

});

把代码上传到服务器 在服务器中安装node 和 pm2

在项目根目录下

执行npm run build 在执行pm2 start --name XXX npm -- start

成功!!完结

相关推荐
小小小小宇4 小时前
虚拟列表兼容老DOM操作
前端
悦悦子a啊4 小时前
Python之--基本知识
开发语言·前端·python
安全系统学习5 小时前
系统安全之大模型案例分析
前端·安全·web安全·网络安全·xss
涛哥码咖5 小时前
chrome安装AXURE插件后无效
前端·chrome·axure
OEC小胖胖5 小时前
告别 undefined is not a function:TypeScript 前端开发优势与实践指南
前端·javascript·typescript·web
行云&流水5 小时前
Vue3 Lifecycle Hooks
前端·javascript·vue.js
Sally璐璐6 小时前
零基础学HTML和CSS:网页设计入门
前端·css
老虎06276 小时前
JavaWeb(苍穹外卖)--学习笔记04(前端:HTML,CSS,JavaScript)
前端·javascript·css·笔记·学习·html
三水气象台6 小时前
用户中心Vue3网页开发(1.0版)
javascript·css·vue.js·typescript·前端框架·html·anti-design-vue
灿灿121386 小时前
CSS 文字浮雕效果:巧用 text-shadow 实现 3D 立体文字
前端·css