简体   繁体   English

为什么我的公用文件夹中的文件得到404

[英]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.

相关问题 为什么我的公用文件夹没有在NodeJS应用程序中获取“ /” URL的文件? - Why isn't my public folder fetching files for '/' URL in my NodeJS App? 为什么未将其保存在公用文件夹中,却在网络中出现404错误? - Why is this not being saved in public folder gives 404 error in Network? 为什么在公用文件夹中找不到我的JavaScript文件? - Why my javascript file is not found in the public folder? node.js express 如何访问我的公共文件夹中的文件 - How to access files in my public folder in node.js express Laravel - Heroku - Docker - 404(未找到)来自公共文件夹的 js css 文件 - Laravel - Heroku - Docker - 404 (Not Found) js css files from public folder 为什么我的 Node.js 网站在公用文件夹中找不到页面? - Why is my Node.js website not finding pages in public folder? 当我使用公共文件夹Symfony 4时,我的css和js文件没有加载 - My css and js files didn't load when i use the public folder Symfony 4 我不能将我的 javascript 文件放在 html 公用文件夹之外吗? - Can't I put my javascript files outside html public folder? 公用文件夹中的Laravel CSS和JS显示404错误 - Laravel CSS and JS in public folder showing 404 error 如何将文件添加到 vue 2 中的“公共文件夹”? - How to add files to the "public folder" in vue 2?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM