首页 文章

无法在Node JS中加载资源

提问于
浏览
0

我正在做一个试图学习Node和Angular的教程 . 我对此完全陌生,我来自LAMP堆栈环境,所以对我来说这是一个全新的世界,我感到完全迷失了 .

我已经安装了Angular JS并将其包含在我的HTML文件中,但我一直收到此错误

http:// localhost:3000 / node_modules / angular / angular.min.js无法加载资源:服务器响应状态为404(未找到)http:// localhost:3000 / app / app.js失败加载资源:服务器响应状态为404(未找到)

EDIT: 我尝试了一些不同的方法,但它们都指向了 /server/ 目录 .

var appDir = path.dirname(require.main.filename);
var appDir = path.dirname(process.mainModule.filename);

这两个都指向 /server/ 目录 .

我的文件夹结构如下:

  • / app

  • app.js

  • / node_modules

  • / server

  • server.js

  • index.html

这是 server.js

var express = require('express');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');

var app = express();

mongoose.connect('mongodb://localhost:27017/social');

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

app.get('/', function(req, res) {
    res.sendFile("index.html", {root: '../'});
});

app.listen('3000', function() {
    console.log('Listening for localhost 3000');
});

这是 app.js

(function() {
    angular.module('Social', []);
}());

这是 index.html

<!DOCTYPE html>
<html lang="en" ng-app="Social">
<head>
    <meta charset="UTF-8">
    <title>The title</title>
</head>
<body>
    <p>Testing the test</p>
</body>

<script src="node_modules/angular/angular.min.js"></script>
<script src="app/app.js"></script>
</html>

1 回答

  • 0

    __dirname 是"The name of the directory that the currently executing script resides in.",这是 server.js 所在的目录 .

    所以当你这样做时:

    express.static(__dirname + '/app')
    

    我认为它实际上是在寻找 /server/app 这是不正确的 .

相关问题