简体   繁体   English

登陆页面不断返回 express 中的静态文件

[英]Landing page keeps returning static files in express

I am trying to use static files in my project, but when the user makes a get request to my "/" landing page.我试图在我的项目中使用静态文件,但是当用户向我的“/”登录页面发出获取请求时。 I want to send non-static things like a json.我想发送非静态的东西,比如 json。 But for some reason it just automatically sends my index.html in my static file.但出于某种原因,它只是自动将我的 index.html 发送到我的静态文件中。

const express = require("express");
const app = express();

app.use(express.static("public"));

app.get("/", (req, res) => {   //This sends the user to the index.html even tho i want to send 123
  res.send("123");
});

app.listen("3000");

As your code is written, the express.static() middleware looks at the / request, finds an index.html file in the public directory and serves that as the response for the / request.在编写代码时, express.static()中间件会查看/请求,在公共目录中找到一个index.html文件,并将其作为/请求的响应。 This is a feature of express.static() that is causing a conflict for you with your custom route for "/" .这是express.static()一个特性,它会导致您与您的"/"自定义路由发生冲突。

You have at least four choices for a solution here:您在这里至少有四个解决方案选择:

  1. You can specify an option for express.static() to disable the index.html feature so it will avoid the "/" route and pass control on to your subsequent route handlers.您可以为express.static()指定一个选项来禁用index.html功能,因此它将避免"/"路由并将控制权传递给后续的路由处理程序。
  2. You can move the express.static() middleware AFTER your app.get("/", ...) route so that your custom route gets first dibs at the request.您可以在app.get("/", ...)路由之后移动express.static()中间件app.get("/", ...)以便您的自定义路由在请求时获得第一个 dib。
  3. You can remove the index.html file from your public directory so express.static() won't find it.您可以从公共目录中删除index.html文件,这样express.static()将找不到它。 Using a template system for all your HTML files that locates all HTML template files in some other directory that express.static() can't see (such as a views directory) would cause this to happen too or just moving it to some private directory and using it from your code from the private directory would work too.对所有 HTML 文件使用模板系统,将所有 HTML 模板文件定位在express.static()看不到的其他目录(例如views目录)中也会导致这种情况发生,或者只是将其移动到某个私有目录并从私有目录中的代码中使用它也可以。
  4. Give ALL your static resources a common path prefix so there is never a URL conflict between static resources and custom route handlers.为所有静态资源提供一个公共路径前缀,这样静态资源和自定义路由处理程序之间就不会出现 URL 冲突。

The first option (disable index.html feature in express.static() ) would look like this:第一个选项(在express.static()禁用 index.html 功能)如下所示:

const express = require("express");
const app = express();

// disable index.html feature
app.use(express.static("public"), {index: false});

app.get("/", (req, res) => {
  res.send("123");
});

app.listen("3000");

The second option (change the order of route definitions/middleware) would look like this:第二个选项(更改路由定义/中间件的顺序)如下所示:

const express = require("express");
const app = express();

app.get("/", (req, res) => {
  res.send("123");
});

// put this after custom route definitions so they take precendence
app.use(express.static("public"));

app.listen("3000");

The fourth option (give all static resources a common path prefix) would look like this:第四个选项(给所有静态资源一个公共路径前缀)看起来像这样:

const express = require("express");
const app = express();

app.use(express.static("/static", "public"));

app.get("/", (req, res) => {
  res.send("123");
});

app.listen("3000");

With this fourth option, you'd then have to prefix all your static URLs with the static prefix (which you can make anything you want) and thus "/" would never be a match for express.static() .使用第四个选项,您必须为所有静态 URL 加上静态前缀(您可以随意设置),因此"/"永远不会与express.static()匹配。

Is there any particular reason that you have an index.html file inside your public directory and are serving something else?您的public目录中有一个 index.html 文件并且正在提供其他服务是否有任何特殊原因? The convention is serve static assets such as CSS, JS, images etc. from your public folder, and HTML / templates in folder which would be a sibling of public such as views .约定是从public文件夹中提供静态资产,例如 CSS、JS、图像等,以及文件夹中的 HTML/模板,这些资源将是public的兄弟姐妹,例如views

The browser will always default to rendering an index.html file if it is found in the root of the public directory.如果在公共目录的根目录中找到 index.html 文件,浏览器将始终默认呈现该文件。 Move the HTML file to a separate folder and the route should work将 HTML 文件移动到一个单独的文件夹,路由应该可以工作

app.use is a middleware, so whenever you're running a app, it always first go through the middleware, Try the below code app.use 是一个中间件,所以每当你运行一个应用程序时,它总是首先通过中间件,试试下面的代码

const express = require("express");
const app = express();

app.get("/", (req, res) => {   //This sends the user to the index.html even tho i want to send 123
  res.send("123");
});

app.use(express.static("public"));

app.listen("3000");

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

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