我想使用 .ejs 将带有 mongoDB 的 node express 应用程序部署到 vercel。
我的文件夹的结构是:
-models
-public
-temp-uploads
-uploads
-views
.env
app.js
db.js
firebase.js
package.json
router.js
vercel.json
-> models 文件夹包含具有从 MongoDB 放入和获取数据功能的文件 -> public 文件夹包含 css 和普通 .js 文件等文件 -> temp-uploads 用于存储要处理的图像的临时文件 -> uploads 用于存储要存储到 firebase 存储的图像 -> views 是所有前端 .ejs 文件所在的位置 -> app.js 包含
let express = require("express");
let app = express();
let session = require("express-session");
let flash = require("connect-flash");
const formidableMiddleware = require("express-formidable-v2");
let sessionOptions = session({
secret: "********",
resave: false,
saveUninitialized: false,
cookie: {
maxAge: 100 * 60 * 60 * 24,
httpOnly: true,
},
});
let router = require("./router");
app.set("views", "views");
app.set("view engine", "ejs");
app.use(flash());
app.use(sessionOptions);
app.use(express.urlencoded({ limit: "25mb", extended: true }));
app.use(express.static(__dirname + "/public"));
app.use(express.json({ limit: "25mb" }));
app.use(formidableMiddleware());
app.use("/", router); //router se connected hai
module.exports = app;
-> db.js 包含
let mongodb = require("mongodb");
const dotenv = require("dotenv");
dotenv.config();
let port = process.env.PORT;
if (port == null || port == "") {
port = 3001;
}
mongodb.connect(
process.env.CONNECTIONSTRING,
{ useNewUrlParser: true, useUnifiedTopology: true },
function (err, client) {
module.exports = client.db();
let app = require("./app");
let server = app.listen(port);
server.keepAliveTimeout = 70000;
server.headersTimeout = 75000;
}
);
-> package.json 包含
{
"name": "Name",
"version": "1.0.0",
"description": "",
"main": "app.js",
"type": "module",
"engines": {
"node": "14.x"
},
"scripts": {
"start": "node db.js",
"test": "echo \"Error: no test specified\" && exit 1",
"watch": "start nodemon db",
"watch2": "start nodemon db --ignore frontend-js --ignore public/ && start webpack --watch"
},
"keywords": [
"Nodejs",
"MongoDB",
"Excel"
],
"author": "",
"license": "ISC",
"dependencies": {
"@babel/core": "^7.14.6",
"@babel/preset-env": "^7.14.7",
"add": "^2.0.6",
"axios": "^0.21.2",
"babel-loader": "^8.2.2",
"compress-images": "^2.0.5",
"connect-flash": "^0.1.1",
"connect-redis": "^7.0.0",
"date-fns": "^2.22.1",
"dotenv": "^10.0.0",
"ejs": "^3.1.7",
"exceljs": "^4.2.1",
"express": "^4.17.1",
"express-formidable": "^1.2.0",
"express-formidable-v2": "^2.2.1",
"express-session": "^1.17.1",
"firebase": "^9.17.1",
"formidable": "^2.1.1",
"fs": "^0.0.1-security",
"gifsicle": "^5.2.1",
"global": "^4.4.0",
"mongo-sanitize": "^1.1.0",
"mongodb": "^3.6.4",
"mongoose": "^6.6.5",
"nodemon": "^2.0.15",
"pngquant-bin": "^6.0.1",
"validator": "^13.7.0",
"webpack": "^5.41.1",
"webpack-cli": "^4.7.2"
}
}
-> router.js 文件包含 api get 和 post 请求 -> vercel.json 包含
{
"version": 2,
"builds": [{
"src": "db.js",
"use": "@vercel/node"
}],
"routes": [
{
"src": "/(.*)",
"dest": "/"
}
]
}
我已经部署,但部署成功后出现“404 - 未”找到错误
请帮助我部署到 vercel。
答案1
尝试重命名db.js
并index.js
更改此行:
app.set("views", "views");
到:
app.set('views', './views');
或者你可以使用https://render.com/,在我看来它是最好的免费部署平台之一。