How to change the folder where socket.io is fetching the socket.io.js JavaScript file? - node.js

The statement
<script src="/socket.io/socket.io.js"></script>
looks for socket.io.js in the 'public' folder instead of node_module.
Changing the url to ../node_module/socket.io/socket.io.js does not work.
How do I change the default folder which src looks into?

socketio sever when receives the request '/socket.io/socket.io.js' , it dynamically builds the content ( considering the transports configured to be supported by the server ).
The file is neither served from public / node modules.

Related

My html cant find my linked javascript event though its in the correct directory, very stumped here

where I link index.js
<script src="/assets/js/index.js"></script>
Folder Directory is such
assests
-css
--style.css
-js
--index.js
enter image description here
Error Im getting
index.html:25 GET file:///assets/js/index.js net::ERR_FILE_NOT_FOUND
I guess you are using an express app(by the tag in question). The static resources such as your javascript, css, images, etc. should be inside the public folder which gets served on running the application. Could you try moving the assets folder inside the public folder and then have a script tag pointing to that resource.
Maybe it helps to remove the first slash:
<script src="assets/js/index.js"></script>
Or startend with a dot
<script src="./assets/js/index.js"></script>

Nodejs file direct access in browser

At Plesk server there are nodejs and reactjs build on hit url the build run but when we hit the nodejs file url of js files it open directly on browser means nodejs files are not secure.
So, it sounds like you are using express.static(), yet the user is able to fetch your server files that are not meant to go to the client. That apparently means that you've pointed express.static at a directory that contains your server files. Instead, you need to point express.static() at a directory hierarchy that ONLY contains files meant to be sent to the client. That means it has to be a separate directory from your server files and it has to not be above your server files directory.
There are many possible places to put it. Here are a couple ways to organize things:
projectDirectory
serverFiles
server.js
clientFiles
index.html
login.html
Then, when running server code from the serverFiles directory, you would use an express.static() like this:
const path = require('path');
app.use(express.static(path.join(__dirname, "../clientFiles")));
Or, you can do it like this:
projectDirectory
serverFiles
server.js
clientFiles
index.html
login.html
const path = require('path');
app.use(express.static(path.join(__dirname, "clientFiles")));
The idea is that the clientFiles directory hierarchy contains only client-side files and express.static() by default will not allow ../ syntax in the URLs to go above it.

Relative path not detected in node.js

I got a http server setup using node.js which responds with an html file in port 3000.This html file has a script tag that returns files using relative path.
Eg html file:
<script src="../helloworld.js"></script>
Now the request.url in node.js http server callback returns only /helloworld.js instead of ../helloworld.js
Node.js file:
var http=require('http');
var fs=require('fs')
http.createServer(function(req,res){
if(req.url=='/')
// read and return html file
else
{
console.log(req.url) // prints /helloworld.js instead of ../helloworld.js
//reading helloworld.js from filesystem
}
}).listen(3000)
From the server side, if the url is rooted, ../ means nothing.
That is:
on the url
www.example.com/notroot
Using
../somefile.js
fetches it from
www.example.com/somefile.js
But if the url is already rooted, ie:
www.example.com
using
../somefile.js
will not work, since there is no parent directory to access.
Also, you don't need the fs component to fetch files from the client side. This is only used to fetch files from the server side (where ../ WILL work).
But as you used I assume that this is a DOM tag embedded in the client's browser and not on the server side.

node routing to html file

This is my folder structure:
root
admin
index.html
bootstrap.css
...
...
app.js
when I'm running
node app.js
using Express 4, routing works correctly but I don't know how to view this file:
http://localhost/admin/index.html
it seems like it searching for this route...
so I tried to render the file to the client and it works but then all its css and script doesn't.
What am I doing wrong?
Please look at:
http://expressjs.com/en/starter/static-files.html
And use:
app.use(express.static('root'));
Express is a framework and provide the template structure, so it's good to put the files of frontend in the folder views, not in static. Static is more used for import libs, js, css.
In you case, put the index.html in the views folder, and the route can be somethink this:
res.sendFile("index.html");
Docs

js file not found on Heroku

My socket.js file can not be found when my Node.js server runs on Heroku, however when it runs on localhost, it is found. In addition, although the app.js file is in the same directory with socket.js file, it can not be found. I've seen some posts suggesting to use
app.use('/', express.static(__dirname));
but i guess it is not the case here.
my index.html file :
<script src="/angular-socket-io/socket.js"></script>
<script src="/socket.js"></script>
<script src="/socket.io/socket.io.js"></script>
Amongst them, the only one couldnt be found is the second directory(/socket.js) which contains the 'socket factory'inside.
My folder tree is as follows;
-app
--assets
---app.js
---socket.js
Any help please?
Just add app.use(express.static(path.join(__dirname, 'app/assets'))); to your main code and after that you can use <script src="/socket.js"></script>
My JavaScript file didn't initialize because of how I imported the JQuery script inside of my index.html.
If your JavaScript file that contains JQuery is not initialized. Make sure to import it with HTTPS not HTTP.
WRONG :
<script src="http://code.jquery.com/jquery-3.5.1.js"></script>
CORRECT :
<script src="https://code.jquery.com/jquery-3.5.1.js"></script>
This solved my problem of missing the JavaScript file on Heroku.

Resources