Javascript 在heroku上部署节点js应用程序,错误:应用程序错误应用程序中发生错误
当在浏览器中的Javascript 在heroku上部署节点js应用程序,错误:应用程序错误应用程序中发生错误,javascript,node.js,express,heroku,Javascript,Node.js,Express,Heroku,当在浏览器中的http://localhost:8080/下引用时,我可以看到铭文helloworld with Express。我正试图将以下申请存入heroku。我遵循了关于heroku的教程 1)创建新应用程序 2) 应用程序名称 3) 欧洲区域 4) heroku登录 5) $cd我的项目/ $git init $heroku git:remote-应用程序 $git add $git commit-am“让它变得更好” $git推送heroku主机 应用程序已生成,我正在尝试运行它。我
http://localhost:8080/
下引用时,我可以看到铭文helloworld with Express
。我正试图将以下申请存入heroku。我遵循了关于heroku的教程
1)创建新应用程序
2) 应用程序名称
3) 欧洲区域
4) heroku登录
5) $cd我的项目/
$git init
$heroku git:remote-应用程序
$git add
$git commit-am“让它变得更好”
$git推送heroku主机
应用程序已生成,我正在尝试运行它。我有一个错误:
应用程序错误应用程序和页面中发生错误
不能上菜。如果您是应用程序所有者,请检查您的日志
详情请参阅。您可以使用以下命令从Heroku CLI执行此操作
api路线
// api-routes.js
// Initialize express router
let router = require('express').Router();
// Set default API response
router.get('/', function (req, res) {
res.json({
status: 'API Its Working',
message: 'Welcome to RESTHub crafted with love!',
});
});
// Import contact controller
var contactController = require('./contactController');
// Contact routes
router.route('/contacts')
.get(contactController.index)
.post(contactController.new);
router.route('/contacts/:contact_id')
.get(contactController.view)
.patch(contactController.update)
.put(contactController.update)
.delete(contactController.delete);
// Export API routes
module.exports = router;
// Import contact model
Contact = require('./contactModel');
// Handle index actions
exports.index = function (req, res) {
Contact.get(function (err, contacts) {
if (err) {
res.json({
status: "error",
message: err,
});
}
res.json({
status: "success",
message: "Contacts retrieved successfully",
data: contacts
});
});
};
// Handle create contact actions
exports.new = function (req, res) {
var contact = new Contact();
contact.name = req.body.name ? req.body.name : contact.name;
contact.gender = req.body.gender;
contact.email = req.body.email;
contact.phone = req.body.phone;
// save the contact and check for errors
contact.save(function (err) {
// Check for validation error
if (err)
res.json(err);
else
res.json({
message: 'New contact created!',
data: contact
});
});
};
// Handle view contact info
exports.view = function (req, res) {
Contact.findById(req.params.contact_id, function (err, contact) {
if (err)
res.send(err);
res.json({
message: 'Contact details loading..',
data: contact
});
});
};
// Handle update contact info
exports.update = function (req, res) {
Contact.findById(req.params.contact_id, function (err, contact) {
if (err)
res.send(err);
contact.name = req.body.name ? req.body.name : contact.name;
contact.gender = req.body.gender;
contact.email = req.body.email;
contact.phone = req.body.phone;
// save the contact and check for errors
contact.save(function (err) {
if (err)
res.json(err);
res.json({
message: 'Contact Info updated',
data: contact
});
});
});
};
// Handle delete contact
exports.delete = function (req, res) {
Contact.remove({
_id: req.params.contact_id
}, function (err, contact) {
if (err)
res.send(err);
res.json({
status: "success",
message: 'Contact deleted'
});
});
};
**contactModel.js**
var mongoose = require('mongoose');
// Setup schema
var contactSchema = mongoose.Schema({
name: {
type: String,
required: true
},
email: {
type: String,
required: true
},
gender: String,
phone: String,
create_date: {
type: Date,
default: Date.now
}
});
// Export Contact model
var Contact = module.exports = mongoose.model('contact', contactSchema);
module.exports.get = function (callback, limit) {
Contact.find(callback).limit(limit);
}
// Import express
let express = require('express');
// Import Body parser
let bodyParser = require('body-parser');
// Import Mongoose
let mongoose = require('mongoose');
// Initialize the app
let app = express();
// Import routes
let apiRoutes = require("./api-routes");
// Configure bodyparser to handle post requests
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
// Connect to Mongoose and set connection variable
mongoose.connect('mongodb://XXXX:XXXX@cluster0-shard-00-00-ov74c.mongodb.net:27017,cluster0-shard-00-01-ov74c.mongodb.net:27017,cluster0-shard-00-02-ov74c.mongodb.net:27017/test123456?ssl=true&replicaSet=Cluster0-shard-0&authSource=admin&retryWrites=true&w=majority', { useNewUrlParser: true});
var db = mongoose.connection;
// Added check for DB connection
if(!db)
console.log("Error connecting db")
else
console.log("Db connected successfully")
// Setup server port
var port = process.env.PORT || 8080;
// Send message for default URL
app.get('/', (req, res) => res.send('Hello World with Express'));
// Use Api routes in the App
app.use('/api', apiRoutes);
// Launch app to listen to specified port
app.listen(port, function () {
console.log("Running App on port " + port);
});
contactController.js
// api-routes.js
// Initialize express router
let router = require('express').Router();
// Set default API response
router.get('/', function (req, res) {
res.json({
status: 'API Its Working',
message: 'Welcome to RESTHub crafted with love!',
});
});
// Import contact controller
var contactController = require('./contactController');
// Contact routes
router.route('/contacts')
.get(contactController.index)
.post(contactController.new);
router.route('/contacts/:contact_id')
.get(contactController.view)
.patch(contactController.update)
.put(contactController.update)
.delete(contactController.delete);
// Export API routes
module.exports = router;
// Import contact model
Contact = require('./contactModel');
// Handle index actions
exports.index = function (req, res) {
Contact.get(function (err, contacts) {
if (err) {
res.json({
status: "error",
message: err,
});
}
res.json({
status: "success",
message: "Contacts retrieved successfully",
data: contacts
});
});
};
// Handle create contact actions
exports.new = function (req, res) {
var contact = new Contact();
contact.name = req.body.name ? req.body.name : contact.name;
contact.gender = req.body.gender;
contact.email = req.body.email;
contact.phone = req.body.phone;
// save the contact and check for errors
contact.save(function (err) {
// Check for validation error
if (err)
res.json(err);
else
res.json({
message: 'New contact created!',
data: contact
});
});
};
// Handle view contact info
exports.view = function (req, res) {
Contact.findById(req.params.contact_id, function (err, contact) {
if (err)
res.send(err);
res.json({
message: 'Contact details loading..',
data: contact
});
});
};
// Handle update contact info
exports.update = function (req, res) {
Contact.findById(req.params.contact_id, function (err, contact) {
if (err)
res.send(err);
contact.name = req.body.name ? req.body.name : contact.name;
contact.gender = req.body.gender;
contact.email = req.body.email;
contact.phone = req.body.phone;
// save the contact and check for errors
contact.save(function (err) {
if (err)
res.json(err);
res.json({
message: 'Contact Info updated',
data: contact
});
});
});
};
// Handle delete contact
exports.delete = function (req, res) {
Contact.remove({
_id: req.params.contact_id
}, function (err, contact) {
if (err)
res.send(err);
res.json({
status: "success",
message: 'Contact deleted'
});
});
};
**contactModel.js**
var mongoose = require('mongoose');
// Setup schema
var contactSchema = mongoose.Schema({
name: {
type: String,
required: true
},
email: {
type: String,
required: true
},
gender: String,
phone: String,
create_date: {
type: Date,
default: Date.now
}
});
// Export Contact model
var Contact = module.exports = mongoose.model('contact', contactSchema);
module.exports.get = function (callback, limit) {
Contact.find(callback).limit(limit);
}
// Import express
let express = require('express');
// Import Body parser
let bodyParser = require('body-parser');
// Import Mongoose
let mongoose = require('mongoose');
// Initialize the app
let app = express();
// Import routes
let apiRoutes = require("./api-routes");
// Configure bodyparser to handle post requests
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
// Connect to Mongoose and set connection variable
mongoose.connect('mongodb://XXXX:XXXX@cluster0-shard-00-00-ov74c.mongodb.net:27017,cluster0-shard-00-01-ov74c.mongodb.net:27017,cluster0-shard-00-02-ov74c.mongodb.net:27017/test123456?ssl=true&replicaSet=Cluster0-shard-0&authSource=admin&retryWrites=true&w=majority', { useNewUrlParser: true});
var db = mongoose.connection;
// Added check for DB connection
if(!db)
console.log("Error connecting db")
else
console.log("Db connected successfully")
// Setup server port
var port = process.env.PORT || 8080;
// Send message for default URL
app.get('/', (req, res) => res.send('Hello World with Express'));
// Use Api routes in the App
app.use('/api', apiRoutes);
// Launch app to listen to specified port
app.listen(port, function () {
console.log("Running App on port " + port);
});
index.js
// api-routes.js
// Initialize express router
let router = require('express').Router();
// Set default API response
router.get('/', function (req, res) {
res.json({
status: 'API Its Working',
message: 'Welcome to RESTHub crafted with love!',
});
});
// Import contact controller
var contactController = require('./contactController');
// Contact routes
router.route('/contacts')
.get(contactController.index)
.post(contactController.new);
router.route('/contacts/:contact_id')
.get(contactController.view)
.patch(contactController.update)
.put(contactController.update)
.delete(contactController.delete);
// Export API routes
module.exports = router;
// Import contact model
Contact = require('./contactModel');
// Handle index actions
exports.index = function (req, res) {
Contact.get(function (err, contacts) {
if (err) {
res.json({
status: "error",
message: err,
});
}
res.json({
status: "success",
message: "Contacts retrieved successfully",
data: contacts
});
});
};
// Handle create contact actions
exports.new = function (req, res) {
var contact = new Contact();
contact.name = req.body.name ? req.body.name : contact.name;
contact.gender = req.body.gender;
contact.email = req.body.email;
contact.phone = req.body.phone;
// save the contact and check for errors
contact.save(function (err) {
// Check for validation error
if (err)
res.json(err);
else
res.json({
message: 'New contact created!',
data: contact
});
});
};
// Handle view contact info
exports.view = function (req, res) {
Contact.findById(req.params.contact_id, function (err, contact) {
if (err)
res.send(err);
res.json({
message: 'Contact details loading..',
data: contact
});
});
};
// Handle update contact info
exports.update = function (req, res) {
Contact.findById(req.params.contact_id, function (err, contact) {
if (err)
res.send(err);
contact.name = req.body.name ? req.body.name : contact.name;
contact.gender = req.body.gender;
contact.email = req.body.email;
contact.phone = req.body.phone;
// save the contact and check for errors
contact.save(function (err) {
if (err)
res.json(err);
res.json({
message: 'Contact Info updated',
data: contact
});
});
});
};
// Handle delete contact
exports.delete = function (req, res) {
Contact.remove({
_id: req.params.contact_id
}, function (err, contact) {
if (err)
res.send(err);
res.json({
status: "success",
message: 'Contact deleted'
});
});
};
**contactModel.js**
var mongoose = require('mongoose');
// Setup schema
var contactSchema = mongoose.Schema({
name: {
type: String,
required: true
},
email: {
type: String,
required: true
},
gender: String,
phone: String,
create_date: {
type: Date,
default: Date.now
}
});
// Export Contact model
var Contact = module.exports = mongoose.model('contact', contactSchema);
module.exports.get = function (callback, limit) {
Contact.find(callback).limit(limit);
}
// Import express
let express = require('express');
// Import Body parser
let bodyParser = require('body-parser');
// Import Mongoose
let mongoose = require('mongoose');
// Initialize the app
let app = express();
// Import routes
let apiRoutes = require("./api-routes");
// Configure bodyparser to handle post requests
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
// Connect to Mongoose and set connection variable
mongoose.connect('mongodb://XXXX:XXXX@cluster0-shard-00-00-ov74c.mongodb.net:27017,cluster0-shard-00-01-ov74c.mongodb.net:27017,cluster0-shard-00-02-ov74c.mongodb.net:27017/test123456?ssl=true&replicaSet=Cluster0-shard-0&authSource=admin&retryWrites=true&w=majority', { useNewUrlParser: true});
var db = mongoose.connection;
// Added check for DB connection
if(!db)
console.log("Error connecting db")
else
console.log("Db connected successfully")
// Setup server port
var port = process.env.PORT || 8080;
// Send message for default URL
app.get('/', (req, res) => res.send('Hello World with Express'));
// Use Api routes in the App
app.use('/api', apiRoutes);
// Launch app to listen to specified port
app.listen(port, function () {
console.log("Running App on port " + port);
});
我解决了。
在package.json
中,我在脚本中添加了“start”:“node index.js”
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js"
}