快递路线原因404

时间:2018-07-04 21:03:20

标签: node.js angular express

快递路由器有问题

我的登录路由为404,所以这一定是初始化路由的方式吗?

我不处理“ /”,但不确定如何处理?

我的索引文件:

var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var blogRoutes = require('./routes/blogs');
var loginRoute = require('./routes/login');

var port = process.env.PORT || '3000';
var http = require('http');
app.set('port', port);


var server = http.createServer(app);
server.listen(port);

mongoose.connect(myInfo);

app.set('view engine', 'hbs'); //Templating engine (HandleBars)
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
  extended: false
}));

app.use(function(req, res, next) {
  res.setHeader('Access-Control-Allow-Origin', '*');
  res.setHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Authorization');
  res.setHeader('Access-Control-Expose-Headers', 'Authorization');
  res.setHeader('Access-Control-Allow-Methods', 'POST, GET, PATCH, DELETE, OPTIONS');
  next();
});
//ROUTES
//Handle specific routes first
app.use('/blogs', blogRoutes);
app.use('/login', loginRoute);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  return res.render('404');
});

module.exports = app;

我的login.js现在只需要简单的日志记录即可:

var express = require('express');
var router = express.Router();

router.get('/', function(req, res, next) {
  console.log("LOG::::::::::::::::::::::::::")
});

在Angular中:

login(email, password) {
    return this.http.post(this.blogsURL + 'login', {email:email, password:password} )
    .map((res) => {
      console.log(res)
      return res;
    })

有指针吗?

2 个答案:

答案 0 :(得分:2)

您正在向GET路由/login发送POST呼叫。

(对不起。我没有足够的声誉来发表评论。)

答案 1 :(得分:0)

抱歉,我犯了一个noobie错误,并且没有订阅该组件中的登录功能

 this.authService.login(email, password)
    .subscribe(data=>console.log(data));

感谢您的帮助