添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams
module.exports = function(app) {
        app.post('/createStation', function(request, response){
            response.redirect('/'); //This doesn't work, why and how to make this work   
           /*var stationDao = require('./server/stationDao.js');
            stationDao.stationDao.createStation(request.body, function(status){
            if(status.status == 'successful'){
                response.redirect('/'); //This is what actually I wanted to do  

Tried using next() as well,

app.post('/createStation', [function(request, response, next){
        var stationDao = require('./server/stationDao.js');
        stationDao.stationDao.createStation(request.body, function(status){
            if(status.status == 'successful'){
                next();
    }, function abc(request, response){
        console.log('I can see this');
        response.redirect('/'); //This doesn't work still
                Have you tried writing the routine with the normal routing syntax, rather  than using self.routeTable?
– strider
                Nov 10, 2015 at 6:45
                you need to set the statusCode to 302 and pass in the url you want to redirect to on the location header of the response en.wikipedia.org/wiki/HTTP_302
– Dayan Moreno Leon
                Nov 10, 2015 at 6:51
                Well, the reason i brought up self.routeTable is that I can't find any official documentation on its use.  Might be worth trying app.post('/createStation',function(req,res){})
– strider
                Nov 10, 2015 at 6:58

On the subject: http://expressjs.com/guide/routing.html#route-handlers

EDIT based on comment:

I've just wrote a really basic server to test what I wrote:

var express = require('express');
var app = express();
app.post('/a', [function(req, res, next) {
  next();
}, function(req, res) {
  res.send('Hello World!');
var server = app.listen(3000, function () { console.log('listening'); });

This works for me, I would encourage you to run that and then curl -X POST http://localhost:3000/a, in my case I correctly got "Hello World!".

If this also works for you try to isolate your problem a bit more by removing some bits and pieces.

I wasn't expecting that :D well... might be express version then, which one are you using? I used the latest. – Alberto Zaccagni Nov 11, 2015 at 16:33 Then I'm sorry but can't help you there :D, I just tried again: created /tmp/server.js and run npm install express from /tmp and it worked fine, could you write the curl command you used to test it? – Alberto Zaccagni Nov 11, 2015 at 17:17

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.