Я пытался запустить nodejs / express api на Elastic Beanstalk.
Когда я запускаю api локально, он отлично работает.
Когда я перехожу к URL-адресу, указанномуasticbeanstalk, я достиг 502 плохого шлюза.
Это журнал ошибок, который я получаю:
Error: Cannot find module '/var/app/current/main.js'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
at Function.Module._load (internal/modules/cjs/loader.js:562:25)
at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! backend@1.0.0 start: `node main.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the backend@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
> backend@1.0.0 start /var/app/current
> node main.js
internal/modules/cjs/loader.js:638
throw err;
Это мой main.js:
const express = require('express')
const app = express()
const bodyParser = require('body-parser')
const cors = require('cors')
const mongoose = require('mongoose')
const passport = require("passport")
// const teamRoutes = express.Router()
const PORT = 4000
//app imports
const { teamRouter } = require('./routers')
const { playerRouter } = require('./routers')
const { reportRouter } = require('./routers')
const { likeRouter } = require('./routers')
const { leagueRouter } = require('./routers')
const { filterRouter } = require('./routers')
const { commentRouter } = require('./routers')
const { adminUserRouter } = require('./routers')
const { tokenRouter } = require('./routers')
let Team = require('./models/Team')
app.use(cors())
app.use(bodyParser.json())
mongoose.connect('mymongodb', { useNewUrlParser: true})
const connection = mongoose.connection
connection.once('open', function(){
console.log('MongoDB database connection established!')
})
// Passport middleware
app.use(passport.initialize());
// Passport config
require("./config/passport")(passport);
app.use('/teams', teamRouter)
app.use('/players', playerRouter)
app.use('/reports', reportRouter)
app.use('/likes', likeRouter)
app.use('/leagues', leagueRouter)
app.use('/filters', filterRouter)
app.use('/comments', commentRouter)
app.use('/api/users', adminUserRouter)
app.use('/tokens', tokenRouter)
app.listen(PORT, function() {
console.log('Server is running on Port: ' + PORT)
})
Thisэто мой package.json:
{
"name": "backend",
"version": "1.0.0",
"description": "",
"scripts": {
"start": "node main.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"axios": "^0.19.0",
"bcryptjs": "^2.4.3",
"body-parser": "^1.19.0",
"concurrently": "^4.1.1",
"cors": "^2.8.5",
"expo-server-sdk": "^3.3.0",
"express": "^4.17.1",
"grunt": "^1.0.4",
"is-empty": "^1.2.0",
"jsonwebtoken": "^8.5.1",
"mongoose": "^5.6.4",
"mongoose-deep-populate": "^3.1.1",
"nodemailer": "^6.3.0",
"passport": "^0.4.0",
"passport-jwt": "^4.0.0",
"validator": "^11.1.0"
}
}
Мой main.js раньше назывался server.js, но я изменил его в соответствии с рекомендацией этого поста: 502 Bad Gateway Развертывание шаблона Express Generator onElastic Beanstalk
Редактировать: Это мой путь к файлу:
Редактировать: Это мой .elasticbeanstalk config.ymlfile:
branch-defaults:
default:
environment: news-arg-backend
group_suffix: null
global:
application_name: news-arg-backend
branch: null
default_ec2_keyname: null
default_platform: Node.js
default_region: us-west-2
include_git_submodules: true
instance_profile: null
platform_name: null
platform_version: null
profile: eb-cli
repository: null
sc: null
workspace_type: Application
В моих .ebextensions у меня есть nodecommand.config и settings.config.
Это мой nodecommad.config:
option_settings:
- namespace: aws:elasticbeanstalk:container:nodejs
option_name: NodeCommand
value: "npm start"
Это мои настройки.конфиг:
option_settings:
aws:elasticbeanstalk:container:nodejs:staticfiles:
option_name: /static/
value: /public/