简体   繁体   English

在Express中使用静态文件进行简单重定向

[英]Simple redirect in Express with static file

I am new to Node and Express. 我是Node and Express的新手。 I've got a static html page where the users posts his username via ajax to my server. 我有一个静态html页面,用户在其中通过ajax将其用户名发布到我的服务器上。 Then I want to redirect him to another html file. 然后,我想将他重定向到另一个HTML文件。

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. 我获得了用户名以及浏览器中的响应,但是服务器未将我重定向到arena.html。 I also don't get any errors. 我也没有任何错误。

Why are these "easy" things so difficult in Node? 为什么这些“简单”的事情在Node中如此困难?

Thank you guys so much for your help. 非常感谢你们的帮助。

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. 在这种情况下,问题在于您似乎已将一些测试(调试?)代码插入到POST路由中,从而阻止了重定向调用的运行。

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 . 摆脱对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. 每当您调用res.end ,它将终止请求,因此在路由中调用之后发生的任何代码都不会运行。

  2. I had to create a new route for /arena . 我必须为/arena创建一条新路线。 This just renders the arena.html file that you've created. 这只会呈现您创建的arena.html文件。 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. 我必须更新您的重定向代码才能将用户实际重定向到/arena (我在第2步中创建的新路由),以便用户随后点击您的/arena路由,最后取回您要向他们展示的模板。

res.redirect函数永远不会执行,因为您正从该语句之前的函数返回。

You pass a URL to res.redirect() . 您将URL传递给res.redirect() That URL should be a URL that you have an appropriate route for that will serve the desired file. 该URL应该是您具有适当路由的URL,该URL将提供所需的文件。

Instead you are doing: 相反,您正在做:

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

But, that isn't a URL at all. 但是,那根本不是URL。 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. res.redirect()将URL发送回浏览器,如果浏览器跟随重定向,它将从头开始请求该URL作为分支新请求。 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. 因此,您需要发送一个URL(而不是路径),并且您需要发送一个URL,您已为其配置了可以提供所需文件的路由。

It also looks like your express.static() statements might be incorrect. 看起来您的express.static()语句可能不正确。 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. 为了使我们能够更具体地提供帮助,我们需要了解静态HTML文件在硬盘驱动器上相对于__dirname ,并且需要确切地了解您希望它们的URL如何工作。 For example, do you want a request for /arena.html to serve __dirname + /public/arena.html ? 例如,是否要请求/arena.html来提供__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. 请说明这一部分,以便我们在您的express.static()语句上提供更具体的建议。

If that is the case, then you can change your redirect to: 如果是这种情况,则可以将重定向更改为:

res.redirect("/arena.html");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM