【问题标题】:how deploy reactjs + nodejs(restify)+ postgresql app on heroku?如何在 heroku 上部署 reactjs + nodejs(restify)+ postgresql 应用程序?
【发布时间】:2020-11-12 13:30:59
【问题描述】:

我使用 reactjs、nodejs(restify) 和 postgresql 创建了应用程序,我在 Heroku 上成功构建,但是当我打开我的应用程序时出现错误 {"code":"ResourceNotFound","message":"/不存在”} 我正在尝试添加 serverStaticFiles() 但它给了我 AssertionError [ERR_ASSERTION]: directory (string) is required 所以我现在删除它现在帮我找到什么我在这里做错了......

Server.js


    var restify=require('restify')
    const { logIn,userCreation, demotable } = require('./routes/Function');
    const corsMiddleware = require('restify-cors-middleware2');
    const { data } = require('jquery');
    const PORT=process.env.PORT || 8080;
    var path=require('path')
    var server=restify.createServer() //server created
    
    server.use(
        function crossOrigin(req,res,next){
          res.header("Access-Control-Allow-Origin", "*");
          res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
          res.setHeader('Access-Control-Allow-Credentials', true); // If needed
          res.header("Access-Control-Allow-Origin", "*");
          res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
          res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
          return next();
        }
      );
    const cors = corsMiddleware({
      preflightMaxAge: 5, //Optional
      origins: ['*'],
      allowHeaders: ['*'],
      exposeHeaders: ['*']
    })
    
    server.pre(cors.preflight)
    server.use(cors.actual)
    //setting
    // if (process.env.NODE_ENV === "production") {
    //   //server static content
    //   //npm run build
    //   server.use(restify.static(path.join(__dirname, "client/build")));
    // }
    
    console.log(__dirname);
    console.log(path.join(__dirname, "client/build"));
    //get data from login form
    
    server.post('/note', userCreation);  
    server.use(restify.plugins.bodyParser());
    server.get('/login',logIn)
    server.get('/employees',demotable)
    
    if(process.env.NODE_ENV==='production')
    {
      server.get('/client/build/*', restify.plugins.serveStatic({
        directory: __dirname,
        default: 'index.html'
       }));
    
    // server.use(restify.serveStatic('client/build'))
    // server.get('*',(req,res)=>{
    //   res.sendFile(path.join(__dirname,'client','build','index.html'))
    // })
    }
    // server.get("*", (req, res) => {
    //   res.sendFile(path.join(__dirname, "client/build/index.html"));
    // });
    server.listen(PORT, function(){
        console.log("server started...")
    })

package.json


    {
      "name": "backend",
      "version": "1.0.0",
      "engines": {
        "npm": "6.x",
        "node": "12.18.2"
      },
      "description": "",
      "main": "index.js",
      "start": "node Server.js",
      "heroku-postbuild": "cd client && npm install && npm run build",
      "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1",
        "start": "node Server.js"
      },
      "author": "",
      "license": "ISC",
      "dependencies": {
        "dotenv": "^8.2.0",
        "jquery": "^3.5.1",
        "pg": "^8.3.0",
        "pg-hstore": "^2.3.3",
        "restify": "^8.5.1",
        "restify-cors-middleware2": "^2.1.0",
        "sequelize": "^6.3.3"
      },
      "devDependencies": {
        "nodemon": "^2.0.4"
      }
    }

【问题讨论】:

    标签: heroku restify heroku-cli


    【解决方案1】:

    您应该将"start": "node server.js" 放在scripts 部分中,例如

    "scripts": {
      "start": "node server.js",
      "test": "echo \"Error: no test specified\" && exit 1"
    },
    

    【讨论】:

    • 您好,我已根据您的建议放入脚本中,但我仍然收到应用程序错误
    • 嗨,我在问题的顶部添加了错误截图,请检查它
    • 当您更改问题时,它会皱眉,因为答案变得毫无意义。这看起来像一个不同的错误,应该单独询问。检查你的package.json 看起来像this
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-22
    • 2019-02-11
    • 2015-03-04
    • 2013-01-08
    • 2017-11-06
    • 1970-01-01
    相关资源
    最近更新 更多