I have a node js backend that I want to deploy on heroku. Here is my server.js
It uses Models, Controllers and Routes and SocketIO.
For the database, it uses MongoDB atlas. All the connection strings are safely stored in env vars
const express = require('express');
const app = express();
const dotenv = require ('dotenv');
const cors = require('cors')
const httpServer = require('http').createServer(app);
const Chat = require('./Model/Chat')
const moment = require("moment");
const bodyparser=require('body-parser');
const connectDB = require('./db/conn');
const io = require('socket.io')(httpServer , {
cors:{
origin:"*",
}
});
dotenv.config({path:'./config.env'});
app.use(express.json());
app.use(cors({
origin:'*'
}))
app.use(bodyparser.urlencoded({extended:true}));
connectDB();
const userRouter = require('./Routes/User')
const auctionRouter = require('./Routes/Auction')
const tripRouter = require('./Routes/Trip')
const vehicleRouter = require('./Routes/Vehicle');
const chatRouter = require('./Routes/Chat')
const notificationRouter = require('./Routes/Notification')
const complaintRouter = require('./Routes/Complaint');
const adminRouter = require('./Routes/Admin');
const accountRequestRouter = require('./Routes/AccountRequest');
app.use('/admin',adminRouter);
app.use('/vehicle' , vehicleRouter);
app.use('/trip', tripRouter)
app.use('/auction', auctionRouter)
app.use('/user', userRouter)
app.use('/chat', chatRouter)
app.use('/notification', notificationRouter)
app.use('/complaint', complaintRouter)
app.use('/accountRequest',accountRequestRouter);
io.on('connection' , (socket) =>{
socket.on('RoomJoin' , async({roomId}) => {
socket.join(roomId);
socket.on('sendMessage',async({senderId,message})=>{
let dateAndTime = moment().format('LLL');
const response = await Chat.findByIdAndUpdate({_id: roomId} , {$push:{chats:{senderId: senderId, message: message , dateAndTime: dateAndTime}}} , {new: true})
socket.emit('getMessages',response)
})
})
})
httpServer.listen(process.env.PORT || 5000 ,()=>{
console.log(`server running on port`);
});
My package.json
{
"name": "node-js-api",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"bcryptjs": "^2.4.3",
"body-parser": "^1.19.0",
"cors": "^2.8.5",
"crypto": "^1.0.1",
"dotenv": "^10.0.0",
"express": "^4.17.1",
"jsonwebtoken": "^8.5.1",
"moment": "^2.29.1",
"mongoose": "^6.0.6",
"mysql": "^2.18.1",
"nodemailer": "^6.6.3",
"socket.io": "^4.2.0",
"twilio": "^3.68.0"
}
}
And the Procfile
web:node server.js
From the logs, I get the following entries:
2021-11-17T11:18:30.558976+00:00 app[web.1]: npm ERR! node-js-api@1.0.0 start: `node server`
2021-11-17T11:18:30.559034+00:00 app[web.1]: npm ERR! Exit status 1
2021-11-17T11:18:30.559105+00:00 app[web.1]: npm ERR!
2021-11-17T11:18:30.559138+00:00 app[web.1]: npm ERR! Failed at the node-js-api@1.0.0 start script.
2021-11-17T11:18:30.559190+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2021-11-17T11:18:30.580305+00:00 app[web.1]:
2021-11-17T11:18:30.580401+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2021-11-17T11:18:30.580429+00:00 app[web.1]: npm ERR! /app/.npm/_logs/2021-11-17T11_18_30_559Z-debug.log
2021-11-17T11:18:30.759991+00:00 heroku[web.1]: Process exited with status 1
2021-11-17T11:18:30.813598+00:00 heroku[web.1]: State changed from starting to crashed
2021-11-17T11:19:56.605489+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=stg-daikyah.herokuapp.com request_id=2f25fbbf-fea1-4715-8b49-93d484e5468f fwd="111.68.111.217" dyno= connect= service= status=503 bytes= protocol=https
2021-11-17T11:19:57.080307+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=stg-daikyah.herokuapp.com request_id=368537f3-146f-42c1-8081-df892b434306 fwd="111.68.111.217" dyno= connect= service= status=503 bytes= protocol=https
Anyone, any ideas???
Aucun commentaire:
Enregistrer un commentaire