无法在 Node JS 中加载资源
Failed to load resource in Node JS
我正在做一个尝试学习 Node 和 Angular 的教程。我对此完全陌生,我来自 LAMP 堆栈环境,所以这对我来说是一个全新的世界,我感到完全迷失了。
我已经安装了 Angular JS 并将其包含在我的 HTML 文件中,但我一直收到此错误
http://localhost:3000/node_modules/angular/angular.min.js Failed to load resource: the server responded with a status of 404 (Not Found)
http://localhost:3000/app/app.js Failed to load resource: the server responded with a status of 404 (Not Found)
编辑: 我尝试了一些不同的方法,但它们都指向 /server/
目录。
var appDir = path.dirname(require.main.filename);
var appDir = path.dirname(process.mainModule.filename);
这两个都指向 /server/
目录。
我的文件夹结构如下:
- /应用程序
- app.js
- /node_modules
/服务器
- 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>
__dirname
是 "The name of the directory that the currently executing script resides in.",这是 server.js
所在的目录。
所以当你在做的时候:
express.static(__dirname + '/app')
我认为它实际上是在寻找 /server/app
这是不正确的。
我正在做一个尝试学习 Node 和 Angular 的教程。我对此完全陌生,我来自 LAMP 堆栈环境,所以这对我来说是一个全新的世界,我感到完全迷失了。
我已经安装了 Angular JS 并将其包含在我的 HTML 文件中,但我一直收到此错误
http://localhost:3000/node_modules/angular/angular.min.js Failed to load resource: the server responded with a status of 404 (Not Found)
http://localhost:3000/app/app.js Failed to load resource: the server responded with a status of 404 (Not Found)
编辑: 我尝试了一些不同的方法,但它们都指向 /server/
目录。
var appDir = path.dirname(require.main.filename);
var appDir = path.dirname(process.mainModule.filename);
这两个都指向 /server/
目录。
我的文件夹结构如下:
- /应用程序
- app.js
- /node_modules
/服务器
- 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>
__dirname
是 "The name of the directory that the currently executing script resides in.",这是 server.js
所在的目录。
所以当你在做的时候:
express.static(__dirname + '/app')
我认为它实际上是在寻找 /server/app
这是不正确的。