Vishal Vishal - 1 year ago 67
AngularJS Question

Why my files in public folder gets 404

I have a file structure like this:

|---- controllers
| |----homeController.js
|---- modules.js
|---- some modules here...
|---- css
| |---main.css
|---- js
| |---main.js
|---- index.html.ejs

Here is my server.js

var express = require('express');
var morgan = require('morgan');
var ejs = require('ejs');

var app = express();


app.use(express.static(__dirname + 'public'));
app.set('view-engine', ejs)

app.get('/', function(req, res) {

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:

<!DOCTYPE html>
<html lang="en">

<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="css/main.css">

<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>


How can I resolve ng folder so that I can reference my angular module and controllers?

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

Answer Source

If you want to serve files in ng directory you should make it public too:

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

Now, you can load the files that are in the ng directory:


By the way it's better to create a folder called app and put everything inside ng in this directory.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download