在Vercel上部署服务器

时间:2020-09-17 07:47:35

标签: node.js express vercel

我正在关注CJ的教程,他使用now部署服务器。

该教程不再是最新的,因为现在已更改为vercel。而且我不知道如何使用vercel。您可以指导我完成此过程吗?我有一个后端和一个前端。我了解CJ的方式是立即部署服务器,而其他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');
});

1 个答案:

答案 0 :(得分:0)

将代码部署到 vercel 后,您需要:

{
  "version": 2,
  "builds": [
    {
      "src": "src/index.js",
      "use": "@vercel/node"
    }
  ],
  "routes": [
    {
      "src": "/(.*)",
      "dest": "src/index.js"
    }
  ]
}

该场景期望您在 index.js 中提到的 package.json 位于 ./src 文件夹下

  • 您可能需要在端口 80 上运行服务器
  • 您需要验证此服务器在本地机器上运行没有问题。
  • 由于您使用的是 builds vercel.json 选项,因此您需要确保所有依赖项,如 node_modules 等都上传到 vercel,方法是从 {{1} },或者使用一些管道或 CI 操作。