当尝试连接到我的NodeJs服务器时,我无法获得任何类型的响应。当我从我的Angular前端尝试POST或GET到我的Node / Express后端时,我得到一个ERR_EMPTY_RESPONSE,如下所示
angular.js:12011 OPTIONS http://localhost:3000/register net::ERR_EMPTY_RESPONSE(anonymous function)
@ angular.js:12011sendReq @ angular.js:11776serverRequest
@ angular.js:11571processQueue
@ angular.js:16383(anonymous function)
@ angular.js:16399$eval @ angular.js:17682$digest
@ angular.js:17495$apply @ angular.js:17790(anonymous function) @ angular.js:25890dispatch
@ jquery.js:5201elemData.handle @ jquery.js:5009
registeruser.js:25 error saving to db
即使我尝试直接连接到节点服务器@ localhost:3000,我也会在服务器没有发送响应的情况下超时。我跟着this tutorial来指导我,并合并了一些东西以使其符合我的需要,我不知道我哪里出错了。
Server.js
// set up ========================
var DATABASE = "mongodb://localhost:27017/smartHomeDevices";
var express = require("express");
var mongoose = require("mongoose"); //require monogDB Driver
var morgan = require("morgan"); // log requests to the console (express4)
var bodyParser = require("body-parser"); // pull information from HTML POST (express4)
var methodOverride = require("method-override"); // simulate DELETE and PUT (express4)
var passport = require("passport");
var _ = require("lodash");
var http = require('http');
//Create App
var app = express();
//Add Middleware for REST API
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json);
app.use(bodyParser.json({
type: 'application/vnd.api+json'
}));
app.use(methodOverride("X-HTTP-Method-Override"));
app.use(morgan("dev"));
app.use(passport.initialize());
//CORS Support, makes API Public
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE");
res.header("Access-Control-Allow-Headers", "Content-Type");
next();
});
//setup
app.models = require("./Models/moduleIndex");
// Bring in the Passport config after model is defined
require('./config/passport');
//registering routes
var routes = require("./routes");
/*_.each(routes, function(controller, route) {
app.use(route, controller(app, route));
});*/
app.use("/", routes);
//err handle
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handlers
// [SH] Catch unauthorised errors
app.use(function(err, req, res, next) {
if (err.name === 'UnauthorizedError') {
res.status(401);
res.json({
"message": err.name + ": " + err.message
});
}
});
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
// Connect to the db
mongoose.connect(DATABASE);
mongoose.connection.once("open", function() {
http.createServer(app, function(req, res) {
console.log(req.url);
res.writeHead(200, {
'Content-Type': 'text/plain'
});
res.end();
}).listen(3000);
module.exports = app;
console.log("Listening on 3000");
});
routes.js
//setup
var express = require('express');
var router = express.Router();
var jwt = require('express-jwt');
var auth = jwt({
secret: 'MY_SECRET',
userProperty: 'payload'
});
var ctrlProfile = require('./Controllers/ProfileController');
var ctrlAuth = require('./Controllers/RegisterUserController');
// profile
router.get('/profile', auth, ctrlProfile.profileRead);
// authentication
router.post('/register', ctrlAuth.register);
router.post('/login', ctrlAuth.login);
module.exports = router;
/*module.exports = {
"/smartDevices" : require("./Controllers/SmartDeviceController"),
"/registeredUsers": require("./Controllers/RegisterUserController")
};*/