【发布时间】:2021-01-04 01:53:27
【问题描述】:
我正在学习CJ 的教程,他使用now 部署他的服务器。
本教程不再是最新的,因为现在已更改为 vercel。而且我不知道如何使用vercel。你能指导我完成整个过程吗?我有一个后端和一个前端。我理解 CJ 的方式是使用 now 部署服务器,而另一个 Tutorial 也使用 now。
我在 velcer 的服务器:link to velcer app
vercel json
{
"name": "my-mongodb-api",
"builds": [{ "src": "index.js", "use": "@now/node-server" }],
"version": 2,
"env": {
"MONGODB_URI": "@my-mongodb-uri"
}
}
json 包
{
"name": "vermittlungsprojekt",
"version": "1.0.0",
"description": "Kunstmuseum Basel",
"main": "index.js",
"dependencies": {
"bad-words": "^3.0.3",
"cors": "^2.8.5",
"express": "^4.17.1",
"express-rate-limit": "^5.1.3",
"monk": "^7.3.1",
"morgan": "^1.10.0"
},
"devDependencies": {
"nodemon": "^2.0.4"
},
"scripts": {
"start": "node index.js",
"dev": "nodemon index.js"
},
"author": "Luis M Luethi",
"license": "ISC"
}
服务器代码
// start server: npm run dev
//
const express = require("express");
const cors = require('cors');
const monk = require("monk");
const Filter = require('bad-words');
const rateLimit = require("express-rate-limit");
const app = express();
const db = monk( 'localhost/beitraege'); /*process.env.my-mongo-uri ||*/
const posts = db.get("post");
const filter = new Filter();
app.use(cors());
app.use(express.json());
app.get("/", (req, res) => {
res.json({
message: "POST"
});
});
app.get("/beitraege", (req, res)=> {
posts
.find()
.then(posts => {
res.json(posts)
})
})
function isValidPost(post){
return post.name && post.name.toString().trim() !== "" &&
post.content && post.content.toString().trim() !=="";
}
app.use(rateLimit({
windowMs: 30 * 1000, // 30 sec
max: 1 // limit each IP to 100 requests per windowMs
}));
app.post("/beitraege", (req, res) => {
if (isValidPost(req.body)){
const post = {
name: filter.clean(req.body.name.toString()),
content: filter.clean(req.body.content.toString()),
created: new Date()
};
//console.log(post);
posts
.insert(post)
.then(createdPost => {
res.json(createdPost);
})
.catch(err => {
return Promise.reject();
})
}else {
res.status(422);
res.json({
message: "Hey, Titel und Inhalt werden benötigt!"
});
}
});
app.listen(5000, () => {
console.log('Listening on http://localhost:5000');
});
【问题讨论】:
-
为同样的问题头疼,如果您找到任何解决方案,请告诉
-
我切换到heroku,它在那里工作。我不知道 vercel 提供什么,但它们不支持节点服务器
-
是吗?但对我来说它有效,api-goloop.vercel.app。你只需要对你的 vercel.json 做一些小调整