[英]Why my files in public folder gets 404
I have a file structure like this: 我有一个像这样的文件结构:
ng
|---- controllers
| |----homeController.js
|---- modules.js
node-modules
|---- some modules here...
public
|---- css
| |---main.css
|---- js
| |---main.js
views
|---- index.html.ejs
package.json
server.js
Here is my server.js 这是我的server.js
var express = require('express');
var morgan = require('morgan');
var ejs = require('ejs');
var app = express();
app.use(morgan('dev'));
app.use(express.static(__dirname + 'public'));
app.set('view-engine', ejs)
app.get('/', function(req, res) {
res.render('index.html.ejs');
});
var server = app.listen(process.env.PORT || 3000, function() {
console.log('server is listening on Port: %d', server.address().port);
});
Now, In my index.html.ejs file: 现在,在我的index.html.ejs文件中:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Mailer</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="css/main.css">
</head>
<body ng-app="addressBookApp">
<h1>Sample Angular App </h1>
<script src="js/main.js"></script>
<script src="ng/modules.js"></script>
<script src="ng/controllers/homeController.js"></script>
</body>
</html>
How can I resolve ng folder so that I can reference my angular module and controllers? 如何解析ng文件夹,以便可以引用我的角度模块和控制器?
Also, As if I run this server then I get error: 另外,好像我在运行此服务器一样,也会出现错误:
Why any files in public folder is not loaded? 为什么没有加载公用文件夹中的任何文件? I get 404 for main.js and main.css
我得到404的main.js和main.css
If you want to serve files in ng
directory you should make it public too: 如果要在
ng
目录中提供文件,也应该将其公开:
app.use(express.static(__dirname + '/ng'));
Now, you can load the files that are in the ng
directory: 现在,您可以加载
ng
目录中的文件:
http://localhost:3000/ng/controllers/homeController.js
By the way it's better to create a folder called app
and put everything inside ng
in this directory. 顺便说一句,最好创建一个名为
app
的文件夹,并将所有内容放在此目录中的ng
中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.