Node project cannot find file, index.js outputs index.html

  node.js, question

clipboard.png

var app = require('express')();
 var http = require('http').Server(app);
 var io = require('socket.io')(http);
 
 app.get('/', function(req, res){
 Res.sendFile(__dirname plus' /index.html');
 });
 
 io.on('connection', function(socket){
 console.log('a user connected');
 });
 
 http.listen(80, function() {
 console.log('listening on *:80');
 });

This is the index.js source code.
Then visit localhost

clipboard.png
Jquery cannot find it, socket.io.js did. . . .
This is the introduction part of index.html

clipboard.png

I understand your question. There are two questions

  1. Why can you find it/socket.io/socket.io.jsThis is because ifSocket.ioThe service listens on your http service and it will automatically providehttp://localhost:<port>/socket.io/socket.io.jsThis route (actually intercepted all/socket.ioAt the beginning of the request, and requestsocket.io.jsWill be resolved tosocket.io-client/socket.io.jsSo you can look at the js you got at the front end, which is actuallysocket.io-clientThe files in the module are notsocket.ioInside the module). You do not need to copy to the external static file directory or provide the service manually.

  2. Why is your own/js/jquery-2.0.3.min.jsBut I can’t find it, because this static file service has no modules for you to provide, so you need to provide it manually, on top of youindex.jsThe code was slightly modified and the results are as follows:

//Change it and propose express
 var express = require('express')
 var app = express();
 var http = require('http').Server(app);
 var io = require('socket.io')(http);
 
 //Provide static file service so that you can find your' jquery-2.0.3.min.js' file
 app.use(express.static(__dirname));
 
 app.get('/', function(req, res){
 Res.sendFile(__dirname plus' /index.html');
 });
 
 io.on('connection', function(socket){
 console.log('a user connected');
 });
 
 //It is better not to directly monitor in HyperText Transport Protocol and change it to 8888
 http.listen(8888, function() {
 console.log('listening on *:8888');
 });
  • Remark: It is better not to directly monitor in80Port, you can listen on other ports and then hang onenginxAs a reverse proxy, this approach may be more relaxed. Direct monitoring at80The port is too violent! Personal opinion, hope to help you.