无法POST /用户/注册

时间:2017-03-31 14:05:54

标签: node.js mean-stack postman

我正在尝试用教程学习MEAN堆栈,当我尝试使用POSTMAN测试post方法时,我仍然坚持在某些方面我已经搜索并尝试了很多方法但还没有找到答案。

代码是:app.js

const express = require('express'),
    path = require('path'),
    bodyParser = require('body-parser'),
    cors = require('cors'),
    passport = require('passport'),
    mongoose = require('mongoose');

const users = require('./routes/users'),
    config = require('./config/database');

//Connect to database
mongoose.connect(config.database);
//On Connection
mongoose.connection.on('connected', () => {
    console.log('Connected to database : ' + config.database);
});
//On Error
mongoose.connection.on('error', (err) => {
    console.log('Database error : ' + err);
});

const app = express();

//Port number
const port = 3000;

//Cors Middleware
app.use(cors());

app.use(express.static(path.join(__dirname, 'public')));

//Body Parser Middleware
app.use(bodyParser.json());

app.use('/users', users);

//Index route
app.get('/', (req, res) => {
    res.send('Invalid response');
});
app.post('/', (req, res) => {
    res.send('Invalid response');
})

//Start Server
app.listen(port, () => {
    console.log('Server stated with port : ' + port);
});

路线代码:users.js

const express = require('express'),
    passport = require('passport'),
    jwt = require('jsonwebtoken'),
    router = express.Router();

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

//Register
router.post('/register', (req, res, next) => {
    let newUser = new User({
        name: req.body.name,
        email: req.body.email,
        username: req.body.username,
        passowrd: req.body.passowrd
    });

    User.addUser(newUser, (err, user) => {
        if (err) {
            res.json({ success: false, msg: 'Failed to register user!' });
        } else {
            res.json({ success: true, msg: 'User Registered' });
        }
    });
});

module.exports = router;

型号代码:user.js

const mongoose = require('mongoose'),
    bcrypt = require('bcryptjs'),
    config = require('../config/database');

//User Schema
const userSchema = mongoose.Schema({
    name: {
        type: String
    },
    email: {
        type: String,
        required: true
    },
    username: {
        type: String,
        required: true
    },
    passowrd: {
        type: String,
        required: true
    }
});

module.exports.addUser = function (newUser, callback) {
    bcrypt.genSalt(10, (err, salt) => {
        bcrypt.hash(newUser.passowrd, salt, (err, hash) => {
            if (err) throw err;
            newUser.passowrd = hash;
            newUser.save(callback);
        });
    });
}

const User = module.exports = mongoose.model('User', userSchema);

POSTMAN config:

方法:POST

URL     http://localhost:3000/users/register

标题     key:Content-Type,     值:application / json

Body
{
    "name": "Wai Lin Aung",
    "email": "wailinaung@mail.com",
    "username": "Wai Lin",
    "passowrd": "123456"
}

POSTMAN输出:

HTML     

Cannot POST /user/register

JSON:     意外的'<'

终端日志:

[nodemon] restarting due to changes...
[nodemon] starting `node app.js`
/Users/wailin/Documents/Projects/meanauthapp/app.js:41
app.post('/', (req,res))
            ^

ReferenceError: req is not defined
    at Object.<anonymous> (/Users/wailin/Documents/Projects/meanauthapp/app.js:41:16)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:393:7)
    at startup (bootstrap_node.js:150:9)
    at bootstrap_node.js:508:3

更新日志:

[nodemon] restarting due to changes...
[nodemon] starting `node app.js`
Server stated with port : 3000
Connected to database : mongodb://localhost:/27017/meanauth
TypeError: User.addUser is not a function
    at router.post (/Users/wailin/Documents/Projects/meanauthapp/routes/users.js:17:10)
    at Layer.handle [as handle_request] (/Users/wailin/Documents/Projects/meanauthapp/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/wailin/Documents/Projects/meanauthapp/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/Users/wailin/Documents/Projects/meanauthapp/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/Users/wailin/Documents/Projects/meanauthapp/node_modules/express/lib/router/layer.js:95:5)
    at /Users/wailin/Documents/Projects/meanauthapp/node_modules/express/lib/router/index.js:281:22
    at Function.process_params (/Users/wailin/Documents/Projects/meanauthapp/node_modules/express/lib/router/index.js:335:12)
    at next (/Users/wailin/Documents/Projects/meanauthapp/node_modules/express/lib/router/index.js:275:10)
    at Function.handle (/Users/wailin/Documents/Projects/meanauthapp/node_modules/express/lib/router/index.js:174:3)
    at router (/Users/wailin/Documents/Projects/meanauthapp/node_modules/express/lib/router/index.js:47:12)
    at Layer.handle [as handle_request] (/Users/wailin/Documents/Projects/meanauthapp/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/Users/wailin/Documents/Projects/meanauthapp/node_modules/express/lib/router/index.js:317:13)
    at /Users/wailin/Documents/Projects/meanauthapp/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (/Users/wailin/Documents/Projects/meanauthapp/node_modules/express/lib/router/index.js:335:12)
    at next (/Users/wailin/Documents/Projects/meanauthapp/node_modules/express/lib/router/index.js:275:10)
    at /Users/wailin/Documents/Projects/meanauthapp/node_modules/body-parser/lib/read.js:129:5

1 个答案:

答案 0 :(得分:0)

以下错误位于app.js.

[nodemon] restarting due to changes...
[nodemon] starting `node app.js`
/Users/wailin/Documents/Projects/meanauthapp/app.js:41
app.post('/', (req,res))

AFAIK req,res是来自express.Router()的属性。但是你只需在你的路由器文件中调用它,而不是在你的app.js