最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Simple redirect in Express with static file - Stack Overflow

programmeradmin0浏览0评论

I am new to Node and Express. I've got a static html page where the users posts his username via ajax to my server. Then I want to redirect him to another html file.

var express = require("express");
var bodyParser = require("body-parser");
var app = express();

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(express.static(__dirname + "/public/arena.html"));

app.get('/',function(req,res){
  res.sendFile(__dirname + "/public/index.html");
});
app.post('/login',function(req,res){
  var username=req.body.username;
  console.log("User name = "+username);
  res.redirect(__dirname + "/public/arena.html");
});

var server = app.listen(3000);

I get the username and also the respond in the browser but the server does not redirect me to arena.html. I also don't get any errors.

Why are these "easy" things so difficult in Node?

Thank you guys so much for your help.

I am new to Node and Express. I've got a static html page where the users posts his username via ajax to my server. Then I want to redirect him to another html file.

var express = require("express");
var bodyParser = require("body-parser");
var app = express();

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(express.static(__dirname + "/public/arena.html"));

app.get('/',function(req,res){
  res.sendFile(__dirname + "/public/index.html");
});
app.post('/login',function(req,res){
  var username=req.body.username;
  console.log("User name = "+username);
  res.redirect(__dirname + "/public/arena.html");
});

var server = app.listen(3000);

I get the username and also the respond in the browser but the server does not redirect me to arena.html. I also don't get any errors.

Why are these "easy" things so difficult in Node?

Thank you guys so much for your help.

Share Improve this question edited Jan 20, 2017 at 17:42 a1xon asked Jan 20, 2017 at 16:21 a1xona1xon 851 gold badge1 silver badge6 bronze badges 3
  • return res.end("wele aboard"); it looks like you're returning from the function before you run the redirect mand (???). I suspect you'll 404 once that code runs because you're going to redirect the user to a path on the file system instead of a path on a website – RyanWilcox Commented Jan 20, 2017 at 16:24
  • you don't write return function before res.redirect(), it defeat the purpose since your function already exit on return. – syarul Commented Jan 20, 2017 at 17:04
  • Did your question get answered? – jfriend00 Commented Mar 24, 2017 at 4:53
Add a ment  | 

3 Answers 3

Reset to default 4

The problem in this case is that it looks like you had some test (debugging?) code inserted into your POST route that is stopping the redirect call from running.

Here's the modified (corrected) version of your program, which will redirect the user in the way you want:

var express = require("express");
var bodyParser = require("body-parser");

var app = express();

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(express.static(__dirname + "/public/arena.html"));

app.get('/', function(req, res) {
  res.sendFile(__dirname + "/public/index.html");
});

app.get('/arena', function(req, res) {
  res.sendFile(__dirname + "/public/arena.html");
});

app.post('/login', function(req, res) {
  var username = req.body.username;
  console.log("User name = " + username);

  // Note how I'm redirecting the user to the /arena URL.
  // When you issue a redirect, you MUST redirect the user
  // to a webpage on your site. You can't redirect them to
  // a file you have on your disk.
  res.redirect("/arena");
});

app.listen(3000);

I had to do a couple of things to get this working:

  1. Get rid of your call to res.end. Whenever you call res.end, it will END the request, so any code that happens AFTER that call in the route will not run.

  2. I had to create a new route for /arena. This just renders the arena.html file that you've created. This is required if you want to 'redirect' the user to an arena page.

  3. I had to update your redirect code to actually redirect the user to /arena (the new route I created in step 2), so that the user will then hit your /arena route, and finally get back the template you are trying to show them.

Your res.redirect function is never executed because you are returning from function right before that statement.

You pass a URL to res.redirect(). That URL should be a URL that you have an appropriate route for that will serve the desired file.

Instead you are doing:

res.redirect(__dirname + "/public/arena.html");

But, that isn't a URL at all. That's a path name on your local hard disk. res.redirect() sends a URL back to the browser and, if the browser is following redirects, it will then request that URL from scratch as a branch new request. So, you need to send a URL (not a path) and you need to send a URL that you have a route configured for that will serve the desired file.

It also looks like your express.static() statements might be incorrect. For us to help more specifically with those, we need to know where the static HTML files are on your hard drive relative to __dirname and we need to know exactly how you want the URLs for them to work. For example, do you want a request for /arena.html to serve __dirname + /public/arena.html? Is that what you are trying to do? Please explain that part so we can advise more specifically on your express.static() statements.

If that is the case, then you can change your redirect to:

res.redirect("/arena.html");
发布评论

评论列表(0)

  1. 暂无评论