Cant find embedded script in HTML in nodemon

Issue

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')
    
})

app.listen(3000)

HTML File :

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    
</head>
<body class = "text-gray-400">
    
    <div>
        <div class = "text-3xl">
            <nav>
                <h1>
                    Hello! Welcome to the first page!
                </h1>
                <h1 class = "chat-notification-title">
                    Hello! Welcome to the first page!
                </h1>
                <form>
                    <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">
                  </form> 
                  <button class ='btn-submit' type = "submit">Add Record</button>
            </nav>
        </div>
    </div><script src="../controller/controller-1.js"></script>
</body>
</html>

Embedded Script :

'use strict'


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

Solution

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.