最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

将node.js服务器部署到heroku

IT培训 admin 13浏览 0评论

将node.js服务器部署到heroku

我遇到错误

应用程序中发生错误,无法提供您的页面。

运行“ heroku open”命令时。在heroku仪表板上,它表示已成功部署,但随后将无法从app.get代码行运行“正在运行”。

server.js

const express = require('express');
const bodyParser = require('body-parser');
const bcrypt = require('bcrypt-nodejs');
const cors = require('cors');
const knex = require('knex');

const register = require('./controllers/register');
const signin = require('./controllers/signin');
const profile = require('./controllers/profile');
const image = require('./controllers/image');

const db = knex({
  client: 'pg',
  connection: {
    host : '127.0.0.1',
    user : 'benjohnson',
    password : '',
    database : 'smart-brain'
  }
});



const app = express();

app.use(cors());
app.use(bodyParser.json());

app.get('/', (req, res)=> { res.send('its working!') })
app.post('/signin', signin.handleSignin(db, bcrypt))
app.post('/register', (req, res) => { register.handleRegister(req, res, db, bcrypt) })
app.get('/profile/:id', (req, res) => { profile.handleProfileGet(req, res, db)})
app.put('/image', (req, res) => { image.handleImage(req, res, db)})
app.post('/imageurl', (req, res) => { image.handleApiCall(req, res)})

app.listen(process.env.PORT || 3000, ()=> {
  console.log(`app is running on port ${process.env.PORT}`);
})

[在heroku cli中运行server.js文件时收到错误,在运行'heroku logs --tail'时我收到此错误?

> [email protected] start /Users/benjohnson/.Trash
> nodemon server.js

sh: nodemon: command not found
npm ERR! file sh
npm ERR! code ELIFECYCLE
npm ERR! errno ENOENT
npm ERR! syscall spawn
npm ERR! [email protected] start: `nodemon server.js`
npm ERR! spawn ENOENT
npm ERR!
npm ERR! Failed at the [email protected] start script.
npm ERR! This is probably not a problem with npm. There is likel
y additional logging output above.
npm WARN Local package.json exists, but node_modules missing, di
d you mean to install?

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/benjohnson/.npm/_logs/2019-09-25T14_11_11_91
0Z-debug.log
回答如下:

错误是关于nodemon。打开package.json,与server.js位于同一文件夹查找:

"scripts": {
    "start": " nodemon server.js",
  },

并替换为:

"scripts": {
    "start": "node server.js",
    "start:dev": "nodemon server.js"
  },

[在Heroku中再次上传。当您想在本地运行项目时,只需在终端npm start:dev中运行,它将使用nodemon加载server.js。

将node.js服务器部署到heroku

我遇到错误

应用程序中发生错误,无法提供您的页面。

运行“ heroku open”命令时。在heroku仪表板上,它表示已成功部署,但随后将无法从app.get代码行运行“正在运行”。

server.js

const express = require('express');
const bodyParser = require('body-parser');
const bcrypt = require('bcrypt-nodejs');
const cors = require('cors');
const knex = require('knex');

const register = require('./controllers/register');
const signin = require('./controllers/signin');
const profile = require('./controllers/profile');
const image = require('./controllers/image');

const db = knex({
  client: 'pg',
  connection: {
    host : '127.0.0.1',
    user : 'benjohnson',
    password : '',
    database : 'smart-brain'
  }
});



const app = express();

app.use(cors());
app.use(bodyParser.json());

app.get('/', (req, res)=> { res.send('its working!') })
app.post('/signin', signin.handleSignin(db, bcrypt))
app.post('/register', (req, res) => { register.handleRegister(req, res, db, bcrypt) })
app.get('/profile/:id', (req, res) => { profile.handleProfileGet(req, res, db)})
app.put('/image', (req, res) => { image.handleImage(req, res, db)})
app.post('/imageurl', (req, res) => { image.handleApiCall(req, res)})

app.listen(process.env.PORT || 3000, ()=> {
  console.log(`app is running on port ${process.env.PORT}`);
})

[在heroku cli中运行server.js文件时收到错误,在运行'heroku logs --tail'时我收到此错误?

> [email protected] start /Users/benjohnson/.Trash
> nodemon server.js

sh: nodemon: command not found
npm ERR! file sh
npm ERR! code ELIFECYCLE
npm ERR! errno ENOENT
npm ERR! syscall spawn
npm ERR! [email protected] start: `nodemon server.js`
npm ERR! spawn ENOENT
npm ERR!
npm ERR! Failed at the [email protected] start script.
npm ERR! This is probably not a problem with npm. There is likel
y additional logging output above.
npm WARN Local package.json exists, but node_modules missing, di
d you mean to install?

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/benjohnson/.npm/_logs/2019-09-25T14_11_11_91
0Z-debug.log
回答如下:

错误是关于nodemon。打开package.json,与server.js位于同一文件夹查找:

"scripts": {
    "start": " nodemon server.js",
  },

并替换为:

"scripts": {
    "start": "node server.js",
    "start:dev": "nodemon server.js"
  },

[在Heroku中再次上传。当您想在本地运行项目时,只需在终端npm start:dev中运行,它将使用nodemon加载server.js。

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论