Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 在Vercel上部署服务器_Node.js_Express_Vercel - Fatal编程技术网

Node.js 在Vercel上部署服务器

Node.js 在Vercel上部署服务器,node.js,express,vercel,Node.js,Express,Vercel,我正在学习来自的教程,他现在使用部署他的服务器 教程不再是最新的,因为现在已改为vercel。我不知道如何与vercel合作。你能指导我完成这个过程吗?我有一个后端和一个前端。我理解CJ的方式是使用now部署服务器,而另一个也使用now 我在velcer的服务器: vercel json { "name": "my-mongodb-api", "builds": [{ "src": "ind

我正在学习来自的教程,他现在使用
部署他的服务器

教程不再是最新的,因为现在已改为vercel。我不知道如何与vercel合作。你能指导我完成这个过程吗?我有一个后端和一个前端。我理解CJ的方式是使用now部署服务器,而另一个也使用
now

我在velcer的服务器:

vercel json

{
    "name": "my-mongodb-api",
    "builds": [{ "src": "index.js", "use": "@now/node-server" }],
    "version": 2,
    "env": {
      "MONGODB_URI": "@my-mongodb-uri"
    }
  }
{
  "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"
}

包json

{
    "name": "my-mongodb-api",
    "builds": [{ "src": "index.js", "use": "@now/node-server" }],
    "version": 2,
    "env": {
      "MONGODB_URI": "@my-mongodb-uri"
    }
  }
{
  "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');
});


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

{
“版本”:2,
“构建”:[
{
“src”:“src/index.js”,
“使用”:“@vercel/node”
}
],
“路线”:[
{
“src”:“/(.*)”,
“dest”:“src/index.js”
}
]
}
该场景期望您在
package.json
中提到的
index.js
位于
/src
文件夹下

  • 您可能需要在端口80上运行服务器
  • 您需要验证此服务器在本地计算机上运行时没有问题
  • 由于您使用的是
    builds
    vercel.json
    选项,因此需要确保所有依赖项,因为
    node\u模块
    等通过从
    vercel cli
    部署文件或使用一些管道或CI操作上传到vercel

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

{
“版本”:2,
“构建”:[
{
“src”:“src/index.js”,
“使用”:“@vercel/node”
}
],
“路线”:[
{
“src”:“/(.*)”,
“dest”:“src/index.js”
}
]
}
该场景期望您在
package.json
中提到的
index.js
位于
/src
文件夹下

  • 您可能需要在端口80上运行服务器
  • 您需要验证此服务器在本地计算机上运行时没有问题
  • 由于您使用的是
    builds
    vercel.json
    选项,因此需要确保所有依赖项,因为
    node\u模块
    等通过从
    vercel cli
    部署文件或使用一些管道或CI操作上传到vercel

为同样的问题伤了我的头,如果你能找到任何解决办法,请告诉我,我换了heroku,成功了。我不知道vercel提供了什么,但他们不支持node Server吗?但对我来说,它奏效了。你只需要对你的vercel.json做一些小的调整就可以了,因为同样的问题打断我的头,如果你找到任何解决方案,请告诉我,我换了heroku,它成功了。我不知道vercel提供了什么,但他们不支持node Server吗?但对我来说,它奏效了。您只需对vercel.json稍作调整