nodejs express的部署简单记录(不详细)

发布时间 2023-06-04 23:38:57作者: 漫漫长路

1. 选用Linux系统
2.node 测试的包有express knex
3. Linux 安装 nodejs 环境 https://github.com/nodesource/distributions
4. Linux 系统权限(sudo) npm 安装 pm2
5. 用 pm2 启动后端服务 示例:pm2 start index.mjs

6.注意端口号占用
7.防火墙打开对应端口号

 

import express from "express";
import Knex from "knex";

const app = express();

const knex = Knex({
client: "mysql2",
connection: {
host: "localhost",
port: 3996,
user: "",
password: "",
database: "",
},
});

// 测试数据库是否连接正常
knex
.raw("SELECT 1")
.then(() => console.log("Connection successful"))
.catch((err) => console.error("Connection failed", err));

app.get("/", (req, res, next) => {
return res.send("hello world!");
});

app.listen(80, () => {
console.log(`http://localhost/`);
});