我是新手并且正在尝试基于nodejs创建和运行应用程序..
通过nodemon运行server.js时,我在控制台中收到以下错误。
Express server listening on port 3000
Mongoose default connection open to mongodb://localhost:27017/foo
GET / 302 18.313 ms - 62
GET / 302 3.115 ms - 62
GET / 302 1.537 ms - 62
GET / 302 1.480 ms - 62
GET / 302 2.280 ms - 62
GET / 302 0.830 ms - 62
GET / 302 0.835 ms - 62
GET / 302 0.895 ms - 62
以下是我的server.js
var path = require('path');
var bodyParser = require('body-parser');
var cors = require('cors');
var fs = require('fs');
var express = require('express');
var logger = require('morgan');
var mongoose = require('mongoose');
var http = require('http');
//var http = require('follow-redirects').http;
var User = require('./models/User');
var Item = require('./models/Item');
var Menu = require('./models/Menu');
var authController = require('./controllers/auth');
var userController = require('./controllers/user');
var menuController = require('./controllers/menu');
var itemController = require('./controllers/item');
var addressController = require('./controllers/address');
var transactionController = require('./controllers/transaction');
var orderController = require('./controllers/order');
var subscriptionMenuController = require('./controllers/subscription');
var config = require('./config');
var async = require('async');
var bcrypt = require('bcryptjs');
mongoose.connect(config.MONGO_URI);
mongoose.connection.on('error', function (err) {
console.log('Error: Could not connect to MongoDB. Did you forget to run `mongod`?'.red);
});
// CONNECTION EVENTS
// When successfully connected
mongoose.connection.on('connected', function () {
console.log('Mongoose default connection open to ' + config.MONGO_URI);
});
// If the connection throws an error
mongoose.connection.on('error',function (err) {
console.log('Mongoose default connection error: ' + err);
});
// When the connection is disconnected
mongoose.connection.on('disconnected', function () {
console.log('Mongoose default connection disconnected');
});
// If the Node process ends, close the Mongoose connection
process.on('SIGINT', function() {
mongoose.connection.close(function () {
console.log('Mongoose default connection disconnected through app termination');
process.exit(0);
});
});
var app = express();
app.set('port', process.env.PORT || 3000);
app.use(cors());
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
// Force HTTPS on Heroku
if (app.get('env') === 'production') {
app.use(function (req, res, next) {
var protocol = req.get('x-forwarded-proto');
protocol == 'https' ? next() : res.redirect('https://' + req.hostname + req.url);
});
}
//app.use(express.static(path.join(__dirname, 'public/dist')));
/*
|--------------------------------------------------------------------------
| Log in with Email
|--------------------------------------------------------------------------
*/
app.post('/auth/login', authController.postLogin);
/*
|--------------------------------------------------------------------------
| Create Email and Password Account
|--------------------------------------------------------------------------
*/
app.post('/auth/signup', authController.postSignup, userController.createUser);
/*
|--------------------------------------------------------------------------
| Create Email and Password Account
|--------------------------------------------------------------------------
*/
app.post('/auth/email', authController.isUserRegistered);
/*
|--------------------------------------------------------------------------
| Login with Facebook
|--------------------------------------------------------------------------
*/
app.post('/auth/facebook', authController.postFacebookLogin);
/*
|--------------------------------------------------------------------------
| Unlink Provider
|--------------------------------------------------------------------------
*/
app.post('/auth/unlink', authController.ensureAuthenticated, authController.postUnlink);
/*
|--------------------------------------------------------------------------
| GET /api/me
|--------------------------------------------------------------------------
*/
app.get('/api/me', authController.ensureAuthenticated, userController.getCurrentUser);
/*
|--------------------------------------------------------------------------
| PUT /api/me
|--------------------------------------------------------------------------
*/
app.put('/api/me', authController.ensureAuthenticated, userController.updateCurrentUser);
/*
|--------------------------------------------------------------------------
| GET /api/menus
|--------------------------------------------------------------------------
*/
app.get('/api/menus', menuController.getMenus);
/*
|--------------------------------------------------------------------------
| GET /api/menus
|--------------------------------------------------------------------------
*/
app.post('/api/menus', menuController.postCreateMenu);
/*
|--------------------------------------------------------------------------
| POST /api/items
|--------------------------------------------------------------------------
*/
app.post('/api/items', authController.ensureAuthenticated, itemController.postCreateItem);
/*
|--------------------------------------------------------------------------
| GET /api/items
|--------------------------------------------------------------------------
*/
app.get('/api/items', authController.ensureAuthenticated, itemController.getItems)
/*
|--------------------------------------------------------------------------
| GET /api/items/:id
|--------------------------------------------------------------------------
*/
app.get('/api/items/:id', authController.ensureAuthenticated, itemController.getItemById)
/*
|--------------------------------------------------------------------------
| PUT /api/items
|--------------------------------------------------------------------------
*/
app.put('/api/items', authController.ensureAuthenticated, itemController.putItems)
/*
|--------------------------------------------------------------------------
| POST /api/addresses
|--------------------------------------------------------------------------
*/
app.post('/api/addresses', authController.ensureAuthenticated, addressController.postCreateAddress);
/*
|--------------------------------------------------------------------------
| GET /api/addresses
|--------------------------------------------------------------------------
*/
app.get('/api/addresses', authController.ensureAuthenticated, addressController.getAddresses)
/*
|--------------------------------------------------------------------------
| GET /api/addresses/:id
|--------------------------------------------------------------------------
*/
app.get('/api/addresses/:id', authController.ensureAuthenticated, addressController.getAddressById)
/*
|--------------------------------------------------------------------------
| PUT /api/addresses
|--------------------------------------------------------------------------
*/
app.put('/api/addresses', authController.ensureAuthenticated, addressController.putAddresses)
/*
|--------------------------------------------------------------------------
| POST /api/orders
|--------------------------------------------------------------------------
*/
app.post('/api/orders', authController.ensureAuthenticated, orderController.postCreateOrder)
/*
|--------------------------------------------------------------------------
| GET /api/orders/:id
|--------------------------------------------------------------------------
*/
app.get('/api/orders/:id', orderController.getOrderById)
/*
|--------------------------------------------------------------------------
| GET /api/allorders
|--------------------------------------------------------------------------
*/
app.get('/api/allorders', orderController.getAllOrders)
/*
|--------------------------------------------------------------------------
| GET /api/orders
|--------------------------------------------------------------------------
*/
app.get('/api/orders', authController.ensureAuthenticated, orderController.getOrders)
/*
|--------------------------------------------------------------------------
| PUT /api/orders/:id
|--------------------------------------------------------------------------
*/
app.put('/api/orders', authController.ensureAuthenticated, orderController.putOrders)
/*
|--------------------------------------------------------------------------
| POST /api/payumoney
|--------------------------------------------------------------------------
*/
app.post('/api/pay', authController.ensureAuthenticated, transactionController.postPay);
/*
|--------------------------------------------------------------------------
| POST /payment/:id/success
|--------------------------------------------------------------------------
*/
app.post('/payment/:id/success', transactionController.paymentSuccess)
/*
|--------------------------------------------------------------------------
| POST /payment/:id/failed
|--------------------------------------------------------------------------
*/
app.post('/payment/:id/failed', transactionController.paymentFailed)
/*
|--------------------------------------------------------------------------
| POST /api/subscriptions
|--------------------------------------------------------------------------
*/
app.post('/api/subscriptions', subscriptionMenuController.postCreateSubscriptionMenu)
/*
|--------------------------------------------------------------------------
| GET /api/subscriptions
|--------------------------------------------------------------------------
*/
app.get('/api/subscriptions', subscriptionMenuController.getSubscriptionMenus)
/*
|--------------------------------------------------------------------------
| GET /api/subscriptions/:id
|--------------------------------------------------------------------------
*/
app.get('/api/subscriptions/:id', subscriptionMenuController.getSubscriptionMenuById)
/*
|--------------------------------------------------------------------------
| PUT /api/menus
|--------------------------------------------------------------------------
*/
app.put('/api/subscriptions', subscriptionMenuController.putSubscriptionMenus)
app.get('*', function(req, res) {
res.redirect('/#' + req.originalUrl);
});
/*
|--------------------------------------------------------------------------
| Start the Server
|--------------------------------------------------------------------------
*/
app.listen(app.get('port'), function () {
console.log('Express server listening on port ' + app.get('port'));
});
以下是我的config.js文件: -
module.exports = {
// App Settings
MONGO_URI: process.env.MONGO_URI || 'mongodb://localhost:27017/foo',
TOKEN_SECRET: process.env.TOKEN_SECRET || 'YOUR_UNIQUE_JWT_TOKEN_SECRET'
}
答案 0 :(得分:1)
302不是错误,它是重定向。你在这做什么
deploy.php
导致无限循环,除非您在某处处理app.get('*', function(req, res) {
res.redirect('/#' + req.originalUrl);
});
路径。