heroku - 如何在 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"
}
}
解决方案
你应该把"start": "node server.js"
里面的scripts
部分像
"scripts": {
"start": "node server.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
推荐阅读
- python - 使用 sudo pip 有多危险?
- javascript - 获取总和为 11 的所有 3 位数字,或者我们可以说模块在 JS 中为 1
- php - 根据 PHP 条件将类应用于两个元素
- reactjs - React - 在渲染时激活声音
- c++ - 每个函数结束时是否需要返回?
- api - 如何管理从 Micronaut 客户端到 REST API 的 JWT 登录过程?
- flutter - BlocProvider.of() 调用的上下文不包含 CounterBloc 类型的 Cubit
- flutter - 如果我在飞镖的某些地方收到空值,如何对字段进行排序?
- arrays - 在二维数组中查找子二维数组的索引,其元素高于阈值
- javascript - Firebase onSnapShot 不断调用