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.
Related
I am new to node.js and am building a chat appication that is running inside an existing PHP application. I am using expressjs and also postman-request module. I can easily hit absolute urls but when I try to request a file that lives on my own file system, it fails. I have watch tutorials and read the docs and it seems like the only examples ever shown are how to hit external urls.. I can't imagine that it is not possible to hit files that reside on your own file system.
Here is code below: (in my main index.js server file)
const request = require('postman-request');
const url = 'utils/config.php'; // this file merely echos out a json encoded string.
request(url, function (error, response, body) {
console.log('error:', error); // Print the error if one occurred
console.log('statusCode:', response && response.statusCode); // Print the response
console.log('body:', body);
});
Here is the error message:
error: Error: Invalid URI "utils/config.php"
This is the file structure:
-node_modules
-public
-src
-utils
-config.php
index.js (start for node.js - inside src folder)
Any help would be appreciated.
Requesting refers to requesting a file from a webserver. Here, you're trying to request a file path, while the computer thinks is a non-existent URL. The PHP file is just text. It doesn't mean anything to the computer, and the PHP interpreter is what runs the PHP code. You aren't running a PHP server in this case, and you're requesting a file path, not a URL.
You have to start a PHP server first, using the php command. Make sure to pass in the URL (for example, localhost:8080/config.php), and not the file path.
If you wanted to do it entirely from the Node script, you could start a server, request the URL, and then stop the server. It would be possible to use the spawn function from the built-in child_process module (refer here). You could also use exec, but this is probably unnecessarily harder.
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.
I'm sorry to ask such a simple question. I've been sent files by someone, an index.html file which pulls in a js file within script tags. I have to start a webserver to get through authentication and view the files (am in dev).
In my CLI i have navigated to the directory containing index.html. I have checked with node -v that I have it installed globally (yes, v 8.6). I've run the simple command node and checked my browser at http://localhost:3000 and a few other ports but get no joy. I've also tried node index.html but CLI throws an error.
How do i start the webserver? All the examples online tell me to build a .js file, but this is not an option.
Steps to set up a node web server
Create the route folder from your local machine.
Go to the command prompt from the project root path.
Install express using the command npm install express
Create server.js file
create the folder wwww and create the Index.html inside it.
server.js
var express = require('express');
var app = express();
app.use(express.static(__dirname + '/www'));
app.listen('3000');
console.log('working on 3000');
Index.html
<!doctype html
<html>
<head>
<title> my local server </title>
</head>
<body>
<h1> server working </h1>
<p> just put your html,css, js files here and it work on your own local nodejs server </p>
</body>
</html>
Go to the project root path and take the command prompt, then start the server by running the command node server.js
Then go to the browser and run the url localhost:3000.
Now you can see the html page will render on your browser.
Since you don't want to build a backend but just an http server.
I would propose to use an npm package that do just what you need:
Open a console
npm install http-server -g
Go to your "index.html" folder (in the console) then type:
http-server
Then reach your content in your browser at this address:
http://localhost:8080
Documentation here:
https://www.npmjs.com/package/http-server
Yes, this is possible.
A very simple example of how to do this would be to create file, let's call it app.js and put this in it:
const http = require('http'), // to listen to http requests
fs = require('fs'); // to read from the filesystem
const app = http.createServer((req,res) => {
// status should be 'ok'
res.writeHead(200);
// read index.html from the filesystem,
// and return in the body of the response
res.end(fs.readFileSync("index.html"));
});
app.listen(3000); // listen on 3000
Now, run node app.js
Browse to http://localhost:3000
There's loads of other npm packages that will help you out do this, but this is the simplest 'pure node' example to literally read index.html and serve it back as the response.
Its very easy to start a server using node js
Create a server.js file,
const http = require('http')
const fs = require('fs');
http.createServer(function (req, res) {
fs.readFile('index.html', function(err, data) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(data);
res.end();
});
}).listen(3000);
Run node server.js
Here is a reference
This will even solve your backslash issue by this
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.
I have just started learning nodejs with the socket.io.js library. My question isn't really related to the stuff in these libraries but rather on how the files are served by a visiting browser.
In my server directory there are just 2 files present (index.html and server.js) along with the node_modules directory (for socket.io). In the index.html I have a script tag including the client side socket.io lib as follows,
<script src="/socket.io/socket.io.js"></script>
The relecvant server code is,
var server = http.createServer(
function(req, res) {
res.writeHead(200, { 'Content-type': 'text/html'});
res.end(fs.readFileSync(__dirname + '/index.html'));
}
).listen(8080,
function() {
console.log('Listening at: http://localhost:8080');
}
);
My question is where is this file present on the server (there is no socket.io directory in the dir where index.html is present)? So how and from where is this being resolved and downloaded correctly by the web browser?
Sorry for the noob question.
The client side file is injected by the socket.io npm module automatically so that when you upgrade the npm module your client side version of socket.io gets updated automatically.
The actual file lives at:
/usr/local/lib/node_modules/socket.io/node_modules/socket.io-client/dist/socket.io.js
Edit: Forgot to mention that when you initialise socket.io you are actualy making it start its own server that serves the file.