0

I am a newbie for nodejs, Kindly excuse me. I have created a node js server app and the html was served using expressjs as follows

app.use(express.static( 'public/js'));
app.use(express.static( 'public/css'));
app.use(express.static( 'public/img'));

res.sendFile(path.join(__dirname +  "/public" + "/login.html"));

In the client side The JS was included in head tag as

<script src="fingerprint.js" type="text/javascript"></script>

The inline JS in the body tag is

var bid = new Fingerprint();
document.getElementById('bid').value = bid.get();
console.log("----"+bid.get());
document.write('Hello');

Here if I run this HTML from the node server, the inline JS was not working, where as if I run in the file mode ie file:///login.html everything is working fine.

4
  • Which directory is fingerprint.js located in? Commented May 7, 2018 at 17:50
  • The path is /public/js/ Commented May 8, 2018 at 5:59
  • 1
    @Zeke Sinder Thanks for your kind participation, It is doing good and It was the problem with the browser plugin. The code is fine with no errors. Commented May 8, 2018 at 6:01
  • Good to hear 👍 Commented May 8, 2018 at 15:54

1 Answer 1

1

This worked for me:

/* jshint ignore:start */
// app.js
const express = require('express');
const app = express();
const path = require('path');

app.use(express.static(__dirname + "/public"));

// Root path redirect
app.get('/', (req, res) => {
    res.sendFile(path.join(__dirname + "/public" + "/login.html"));
});

app.listen(3000, () => console.log('Example app listening on port 3000!'))

Here's what my directory structure looks like (checkout the left pane): VS Code Screenshot

The HTML file:

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="/js/fingerprint.js"></script>
</head>

<body>
    <!-- HTML Content  -->
    <div id="bid"></div>
    <!-- Inline JS -->
    <script>
        var bid = new Fingerprint();
        document.getElementById('bid').value = bid.get();
        console.log("The Bid Value: " + bid.get());
        // document.write('Hello');
    </script>
</body>

</html>

Finally, the fingerprint.js file:

/* jshint ignore:start */
class Fingerprint {
    get() {
        return 2;
    }
}

I can see the Fingerprint object loading correctly:

Chrome Dev Tools

Let me know if this works for you.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @benSooraj, The problem was the noscript plugin which was installed in FF.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.