Kevin Hu
Kevin Hu

Reputation: 31

index.html not invoking javascript file

I'm making a simple client/server connection via node.js modules and a simple HTML page.

the html page is this:

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

Where the index.js file in the same directory is this:

alert("hello!");

This works fine when I manually click on the html page, but when I invoke it using my app.js:

var express = require("express");
var app = express();
var server = require('http').Server(app);

app.get('/', function (req, res) {
    res.sendFile(__dirname + '/web/index.html');
});
app.use('/web', express.static(__dirname + '/web'));
server.listen(2000); 

console.log('Started Server!');

by calling

node app.js

it does not show the alert when the HTML page loads. I just installed node.js and installed the node express dependencies in this app after I called "node init" on the project.

Upvotes: 0

Views: 200

Answers (1)

smhrjn
smhrjn

Reputation: 546

The path for index.js and static folder does not match. Therfore it fails to load index.js.

This should work:

app.use('/', express.static(__dirname + '/web'));
server.listen(2000);

Upvotes: 2

Related Questions