Express-js无法获取我的静态文件,为什么? [英] Express-js can't GET my static files, why?

查看:163
本文介绍了Express-js无法获取我的静态文件,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经将我的代码缩小到我可以做的最简单的express-js应用程序:

I've reduced my code to the simplest express-js app I could make:

var express = require("express"),
    app = express.createServer();
app.use(express.static(__dirname + '/styles'));
app.listen(3001);

我的目录如下所示:

static_file.js
/styles
  default.css

但是当我访问 http:// localhost:3001 / styles / default.css 时,我收到以下错误:

Yet when I access http://localhost:3001/styles/default.css I get the following error:

Cannot GET / styles /
default.css

我正在使用 express 2.3.3 节点0.4.7 。我做错了什么?

I'm using express 2.3.3 and node 0.4.7. What am I doing wrong?

推荐答案

尝试 http:// localhost:3001 / default.css

要在您的请求网址中使用 / styles ,请使用:

To have /styles in your request URL, use:

app.use("/styles", express.static(__dirname + '/styles'));

查看此页面

//Serve static content for the app from the "public" directory in the application directory.

    // GET /style.css etc
    app.use(express.static(__dirname + '/public'));

// Mount the middleware at "/static" to serve static content only when their request path is prefixed with "/static".

    // GET /static/style.css etc.
    app.use('/static', express.static(__dirname + '/public'));

这篇关于Express-js无法获取我的静态文件,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆