Cant find embedded script in HTML in nodemon


I am a newbie, experimenting with JS. I was trying to build a small Node.js app, with a JS script embedded in the index.html, but when I run nodemon, it returns 404 for the script and cant find it. The same html page with embedded script works fine with parcel. What am I doing wrong?

My app.js :

const express = require('express')
app = express()

app.get('/', (req, res, next)=>{
    res.sendFile('D:/Node.js Folder/My first project/views/index.html')

app.get('/add-user', (req, res, next)=>{
    res.sendFile('D:/Node.js Folder/My first project/views/add-user.html')

app.get('/show-user', (req, res, next)=>{
    res.sendFile('D:/Node.js Folder/My first project/views/show-users.html')


HTML File :

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
<body class = "text-gray-400">
        <div class = "text-3xl">
                    Hello! Welcome to the first page!
                <h1 class = "chat-notification-title">
                    Hello! Welcome to the first page!
                    <label for="cname">Company Name</label><br>
                    <input type="text" id="cname" name="cname"><br>
                    <label for="ccode">Company Code</label><br>
                    <input type="text" id="ccode" name="ccode">
                  <button class ='btn-submit' type = "submit">Add Record</button>
    </div><script src="../controller/controller-1.js"></script>

Embedded Script :

'use strict'

const button = document.querySelector('.btn-submit')
button.addEventListener('click', function () {
    console.log('BUTTON PRESSED')


One thing you need to do is stop using absolute paths

for eg. 'D:/Node.js Folder/My first project/views/index.html'

should be ./views/index.html

assuming that the app.js file is in the same folder as the views folder.

The server on which you might deploy this code is not going to have the same path as your pc.

You also might wanna make a static folder and move your html, script (controller) folder inside that
and add the following line to the app.js

app.use('/', express.static('static'))

// or

app.use('/', express.static('./static'))

this way you won’t have to serve the individual files like you are doing right now.

if you have the src for the embedded file set to ./controller/controller-1.js it will work fine.

Answered By – SuperSaiyan99000

Answer Checked By – Dawn Plyler (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.