首页 > 解决方案 > 如何在 heroku 上部署 reactjs + nodejs(restify)+ postgresql 应用程序?

问题描述

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

服务器.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...")
    })

包.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"
      }
    }

标签: herokurestifyheroku-cli

解决方案


你应该把"start": "node server.js"里面的scripts部分像

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

推荐阅读