Trying to Load a Script File from My Node Server - Get 404

Node JS get 404 error not found when i try to access to a js file

You are telling your express server to serve the folder 'Server/client/js' at route 'localhost:3000/js'.

If your folder structure is what you say it is, then you would get this error, since there is no such folder.

The most straightforward way of fixing this would be to move your client folder into your Server folder and change your static folder destination to './Client/js'.

Receiving 404 status when trying to load local files with NodeJS

The reason for the mime error is actually confusing - it's due to the browser not being able to find that file.

Remove the /public part of the path location as that is the folder so it's being served from:

<link rel="stylesheet" type="text/css" href="/stylesheets/style.css" />

Same for all your other static files: /assets/mypic.png etc

Update your location for the static path as follows:

app.use(express.static(path.join(__dirname, 'public'), {index: false}));

Then, update sendFile to use and absolute path as follows:

return res.sendfile(path.resolve('./index.html'));
// or
return res.sendfile(path.join(__dirname, 'index.html'));

How to solve net::ERR_ABORTED 404 in Nodejs

You shouldn't use <script> tags to import server side files like for an example server.js, this file is used to run the node server

let's say you make a script for home page, you need to save it inside /public and the send it to client to be interpreted by the browser by adding it to partials/scripts.ejs

example

<script type="javascript" src="/home_script.js"></script>

the path would be

public/home_script.js

Edit:
it feels like you're still a bit confused so i'll take a example

in server.js you have

const main = require("./routes/main");
app.use("/", main);

think about the file main.js like taking a function from server.js and moving it to a single file

now in main.js i'm guessing you have a function like this:

router.get('/', (req,res,next) => {
res.render('home.ejs')

})

the code above is part of server side code and shouldn't be sent to the user(the client)

now inside home.ejs
you have your partials and then a section for scripts

<script type="javascript" src="/bootstrap.bundle.min.js"></script>
<script type="javascript" src="/home_script.js"></script>

this file home_script should contains stuff that you want to do once the page arrives the user (we call this client side)

as an example:

if you have button and you want to do something when you click you write that part of javascript inside home_script.js

Getting 404 Not Found error when including scripts using Node.js

if you want to serve up static content with express you need to do something like...

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

where the arg to express.static is the dir with your static content.



Related Topics



Leave a reply



Submit