连续发布请求节点JS

时间:2016-10-31 00:12:26

标签: javascript node.js express post routing

我想知道 我可以在单独的路径文件中执行连续的发布请求,并且每个发布请求都会重定向到不同的ejs页面。

这是我的文件结构

路线:-index.js

-users.js

-users1.js

的观点:-index.ejs

-users.ejs

-users1.ejs

这是我的app.js

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

var routes = require('./routes/index');
var users = require('./routes/users');
var users1 = require('./routes/users1');
var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

// 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')));

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

// catch 404 and forward to error handler

app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
  app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
      message: err.message,
      error: err
    });
  });
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
  res.status(err.status || 500);
  res.render('error', {
    message: err.message,
    error: {}
  });
});


module.exports = app;

这是我的index.js

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

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index');
});
router.post('/users', function(req, res, next) {
  res.redirect('/users');
});
module.exports = router;`

这是我的index.ejs:

<!DOCTYPE html>
<html>
  <head>
    <title></title>
    <link rel='stylesheet' href='/stylesheets/style.css' />
  </head>
  <body>
    <form action="/users" method="post">
    <input type="submit" value="post">
     </form>
  </body>
</html>

这是我的users.js

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

/* GET users listing. */
router.get('/', function(req, res, next) {
  res.render('users');
});
router.post('/users1', function(req, res, next) {
  res.redirect('/users1');
});
module.exports = router;

这是我的users.ejs

    <html>
  <head>
    <link rel='stylesheet' href='/stylesheets/style.css' />
  </head>
  <body>
     <form action="/users1" method="post">
    <input type="submit" value="post">
     </form>
  </body>
</html>

这是我的users1.js

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

/* GET home page. */
router.get('/', function(req, res, next) {

  res.render('users1');
});
module.exports = router;

这是我的users1.ejs

   <!DOCTYPE html>
<html>
  <head>
    <title></title>
    <link rel='stylesheet' href='/stylesheets/style.css' />
  </head>
  <body>
<h1>hello</h1>
  </body>
</html>

从index.ejs到users.ejs的post请求工作正常,它正在重定向到users.ejs 但users.ejs对users1.ejs的请求不起作用。 它显示404未找到 这是整个console.log输出

> hostelSystem@0.0.0 start /home/ritwik/hostelSystem
> node ./bin/www

POST /users 302 43.621 ms - 56
GET /users 200 10.041 ms - 205
GET /stylesheets/style.css 304 2.742 ms - -
POST /users1 404 16.965 ms - 946

1 个答案:

答案 0 :(得分:0)

在你的users1路由器中看起来像你正在使用router.get你可能想使用router.post,因为你请求domain.com/users1它只直接到这个路由器,我只是注意到你现在不需要转发,因为当您调用domain.com/users时,它会直接转到用户路由器

router.post('/', function(req, res, next) {
    res.render('users1');
});
module.exports = router;