简体   繁体   中英

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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM