ExpressJS在POST后重定向

时间:2017-04-25 08:29:07

标签: mongodb express redirect post

我有以下问题。我用ExpressJs编写的服务器(使用MongoDB作为数据库)在注册后重定向我的页面(POST方法)。为什么呢?

Server.js

const express    = require('express');
const bodyParser = require('body-parser');
const app        = express();
const morgan     = require('morgan');

app.use(morgan('dev')); 
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

const port = process.env.PORT || 9090;

const mongoose   = require('mongoose');
mongoose.connect('mongodb://localhost/db'); 
const router = express.Router();

app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  next();
});

router.get('/', function(req, res) {
    res.json({ 
    "get | post": "/users"
   });  
});

app.use('/api', router);
require('./routes/users')(app,router);

app.listen(port);
console.log('Started server on port ' + port);

users.js

const User = require('../models/user');

module.exports = function(app, router){
    app.post('/api/users',function(req, res) {
        var user = new User();
        user.name = req.body.name;
        user.surname = req.body.surname;
        user.email = req.body.email;
        user.age = req.body.age;
        user.login = req.body.login;
        user.password = req.body.password;
        user.created_at = req.body.created_at;
        user.update_at = req.body.update_at;
        user.save(function(err) {
            if (err)
                res.send(err);
            res.json({ 
                name: user.name,
                surname: user.surname,
                email: user.email,
                age: user.age,
                login: user.login,
                password: user.password,
                update_at: user.update_at,
                created_at: user.created_at
            });
        });
    })
    app.get('/api/users',function(req, res) {
        User.find(function(err, users) {
            if (err)
                res.send(err);

            res.json(users);
        });
    });
}

user.service.ts

import { Observable } from 'rxjs/Observable';
import { Http, RequestOptions, Headers } from '@angular/http';
import { Injectable } from '@angular/core';
import { db_url } from './../../../assets/db_url';
import { User } from './../../../assets/interfaces/db.interface';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/toPromise';
@Injectable()
export class UserService {

    constructor(private _http: Http) {}
    registerUser(user_data: User): Observable<any>{
        let headers = new Headers({ 'Content-Type': 'application/json' });
        let options = new RequestOptions({ headers: headers });
        let url = "http://localhost" + db_url + "/users";
        return this._http.post( url,  user_data , options )
                        .map(data => data.json())
                        .catch(err => err);
    }
}

1 个答案:

答案 0 :(得分:0)

尝试将return运算符添加到相同位置

app.get('/api/users',function(req, res) {
    User.find(function(err, users) {
        if (err)
            return res.send(err); <<< 

        res.json(users);    
    });
});