简体   繁体   English

我正在尝试从本书中运行Express静态中间件,但看不到该示例试图显示的内容

[英]I'm trying to run an Express static middleware from this book, and I cannot see what the example is trying to show

I'm on a section in this book about Express where it mentions express.static to serve files, but each time I run it, I get the code catching when no file is found. 我在这本书的有关Express的一节中提到了express.static来提供文件,但是每次运行它时,都会在找不到文件的情况下捕获代码。 I have made a folder called public and put in an HTML file, and had it both in the project folder and above it but nothing happens other than the message given after it runs the static method call. 我已经建立了一个名为public的文件夹,并放入了HTML文件,并将其放在项目文件夹中及其上方,但是除了运行静态方法调用后给出的消息外,没有任何反应。

The code is: 代码是:

var express = require("express");
var path = require("path");
var http = require("http");

var app = express();

var publicPath = path.resolve(__dirname,"public");
app.use(express.static(publicPath));

app.use(function(request,response){
    response.writeHead(200,{"Content-Type":"text/plain"});
    response.end("Looks like you didn't find a static file");
});

http.createServer(app).listen(3000);

The book does not give much more explanation. 这本书没有给出更多的解释。 I've never worked with servers. 我从未使用过服务器。 What is going on? 到底是怎么回事?

I ran your sample code and was able to get it to display HTML files created in a public directory in the project root. 我运行了您的示例代码,并能够显示在项目根目录的public目录中创建的HTML文件。

Eg. 例如。 public/page.html could be retrieved at localhost:3000/page.html 可以在localhost:3000/page.html检索public/page.html localhost:3000/page.html

If you create an index.html file in your public directory it will be retrieved at localhost:3000 . 如果在公共目录中创建index.html文件,它将在localhost:3000处进行检索。 The index does not need the full .html extension. 索引不需要完整的.html扩展名。

暂无
暂无

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

相关问题 我尝试在 javascript 中创建一个 while 循环。 我有我想在身体上做什么的例子 - I try to make a while loop in javascript. I have example of what i'm trying to do in body 我在尝试控制台 object 书时收到“”Uncaught SyntaxError: Unexpected identifier“”问题出在 onclick="alertCLick(book)" - I'm getting ""Uncaught SyntaxError: Unexpected identifier"" while trying to console the book object the problem is in the onclick="alertCLick(book)" 我在尝试运行构建时遇到问题 - I'm facing a problem when I'm trying to run build 我正在尝试通过 express 为我的 React 应用程序提供服务,为什么我会收到 404? - I'm trying to serve my react app from express, why am I getting a 404? 我正在尝试创建一个简单的快速应用程序,但似乎无法正常工作 - I'm trying to create a simple express application but it seems not to be working 我正在尝试 HTML5 教程中的 sessionStorage 示例,但它不起作用,“rightbox”部分没有任何变化 - I'm trying this example of sessionStorage from an HTML5 tutorial but it is not working and nothing is changing in the 'rightbox' section 我正在尝试发送带有 node、nodemon、express 和 dotenv 的 email - I'm trying to send an email with node, nodemon, express and dotenv 我试图运行命令 Discord.js 但出现错误“TypeError:无法读取未定义的属性'get'” - i m trying to run a command Discord.js but error appear “TypeError: Cannot read property 'get' of undefined” 我正在尝试将TypeScript的示例转换为JavaScript,但不确定如何将其转换 - I'm trying to translate an example of TypeScript into JavaScript, and I'm not sure how to convert it 我正在尝试在菜单单击上隐藏/显示div - I'm trying to hide/show divs on menu click
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM