gpt4 book ai didi

node.js - 无法使用 Express Router() 进行 GET/POST

转载 作者:太空宇宙 更新时间:2023-11-03 23:30:43 25 4
gpt4 key购买 nike

所以我已经尝试解决这个问题有一段时间了,但我在这里遗漏了一些东西。我正在尝试确定在 Express 中设置路线的最佳实践。我想通过文件分隔我设置的不同路线。我正在尝试使用 Express Router 模块,但无法让它工作。这是我的主服务器文件:

var express      = require('express');
var handlebars = require( 'express-handlebars' );
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

// Models
var db = require( './models/db.js' );
var user = require( './models/user.js' );

// Routes/Controllers
var routes = require( './routes/index.js' );
var userRouter = require( './routes/users.js' );

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.engine( 'handlebars', handlebars( { defaultLayout: 'main' } ) );
app.set('view engine', 'handlebars');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

// Routing Table
app.use('/', routes);

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

以下是用户的路线:

var express        = require('express');
var userRouter = express.Router();
var mongoose = require( 'mongoose' );
var bodyParser = require( 'body-parser' );
var methodOverride = require( 'method-override' );

userRouter.use( bodyParser.urlencoded({ extended: true }));
userRouter.use( methodOverride( function( req, res ) {
if ( req.body && typeof req.body === 'object' && '_method' in req.body ) {
var method = req.body._method;
delete req.body._method;
return method;
}}));


/* GET users listing. */
userRouter.get( '/users', function( req, res, next ) {
console.log( 'users GET' );
mongoose.model( 'User' ).find( {}, function( err, users ) {
if( err ) {
console.error( err );
} else {
console.log( users );
res.render( 'users/index', { users: users } );
}
});
})
.post( function( req, res, next ) {
console.log( req );
});

module.exports = userRouter;

这是我尝试发布的表单:

<form name='create-users' action='/users' method='post' >
<h6>Create Account</h6>
<fieldset>
<label for='username'>User Name</label>
<input name='username' id='newname' type='text' placeholder='User Name'>
<label for='email'>Email</label>
<input name='email' id='email' type='text' placeholder='Email'>
<label for='newpassword'>Password</label>
<input name='password' id='newpassword' type='text' placeholder='Password'>
<button type='submit'>Create</button>
</fieldset>
</form>

我知道我不需要正确的文件,但这只是给我一个 404 错误。

最佳答案

在你的第二个文件中,只需监听/路由:

 /* GET users listing. */
userRouter.get( '/', function( req, res, next ) {
console.log( 'users GET' );
mongoose.model( 'User' ).find( {}, function( err, users ) {
if( err ) {
console.error( err );
} else {
console.log( users );
res.render( 'users/index', { users: users } );
}
});
})
.post('/', function( req, res, next ) {
console.log( req );
});

由于您正在第一个文件 app.use("/users"..) 中监听

关于node.js - 无法使用 Express Router() 进行 GET/POST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38508238/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com