ThePirateBae
ThePirateBae

Reputation: 89

Why am i getting a 404 js file, while including a script tag?

index.html

<head>
    <script src="/main.js"></script>
</head>

Error:

GET http://localhost:3000/main.js 

Structure

I've tried src="main.js". /view/main.js

Very basic, but dont want to get stuck on this any longer... sigh.

if it helps my app.js file has this:

app.get('/', function (req, res) { res.sendFile(__dirname + '/view/home.html'); });

Upvotes: 1

Views: 1309

Answers (1)

Artem Arkhipov
Artem Arkhipov

Reputation: 7455

So, according to your comments - you are serving only the 'index.html' file instead of whole directory. Try this code:

var express = require('express');
var path = require('path');
var app = express();

app.use(express.static(path.join(__dirname, 'view')));

//... other settings and server launching further

If you want to set serving static files to particular route - extend 'app.use' line with '/your-route', like this:

app.use('/your-route', express.static(path.join(__dirname, 'view')));

After that you can use <script src="main.js"></script> in your index.html

Upvotes: 2

Related Questions