node.js - Express.js index.js 404(未找到)

标签 node.js express

出于某种原因,它无法加载从我的 index.html 引用的 index.js 脚本。 server.js、index.html 和 index.js 都位于我的项目文件夹的根目录下。

server.js

var express = require('express'),
    app = express();

app.set("view options", {layout: false});
app.use(express.static(__dirname + '/public'));
app.set('views', __dirname + '/');
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');

app.get('/', function (req, res) {
    res.render('index.html');
});

app.listen(4000, function () {
    console.log('Example app listening on port 4000!');
});

index.html

<html>
    </head></head>
    <body>
        <div class="ink-grid vertical-space">
            <div id="content">
                <div class="panel vertical-space">
                    <div id="app"/>
                </div>
            </div>
        </div>
        <script type="text/babel" src="index.js"></script>
    </body>
</html>

最佳答案

您必须提供这些文件。您可以单独进行:

app.get('/index.js', function (req, res) {
    res.sendFile('index.js');
});

或者为您要提供的所有内容创建一个目录(例如 www)并使用 express.static:

app.use(express.static('www'));

关于node.js - Express.js index.js 404(未找到),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36395631/

相关文章:

node.js - NodeJS 中 Mongoose 总是返回空数组

javascript - 从 github 克隆应用程序——缺少模块 depencines。如何一次性全部安装?

node.js - 无法运行 nools

javascript - node.js/socket.io,cookie 只在本地工作

node.js - 即使提供了文件, Mongoose 也会出现验证错误

node.js - Amazon S3 访问仅在 CopyObject 中被拒绝

node.js - React Express Fetch 发布 CORS 错误 : Response to preflight request doesn't pass access control check: It does not have HTTP ok status

javascript - 使用 Node.js 和 Mailgun 发送 HTML 时遇到问题

angularjs - mean.js 菜单 isPublic 不起作用

javascript - 如何将数据数组从express.js传递到ejs模板并渲染它们