我正在尝试通过教程学习 MEAN 堆栈,当我尝试使用 POSTMAN 测试帖子方法时,我坚持了一段时间,我已经搜索并尝试了很多方法,但还没有找到答案。
代码是:应用程序.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);
});
路由代码:用户.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;
型号代码:用户.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);
邮递员配置:
方法 : 开机自检
网址 http://localhost:3000/users/register
页眉 键: 内容类型 , 值:应用程序/JSON
Body
{
"name": "Wai Lin Aung",
"email": "wailinaung@mail.com",
"username": "Wai Lin",
"passowrd": "123456"
}
邮递员输出:
.HTML
无法开机自检/用户/注册
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
下面的错误在应用程序中.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 的属性。路由器((。但是您只需在路由器文件中调用它,而不是在您的应用程序中调用它.js