Node.js source code getting sent over to client - javascript

I'm building with Node.js locally and I came along a weird problem where if I typed localhost:8080/server.js, the whole server-side source code shows up in the browser (the server file's name is server.js). I guess what's happening is the server looks for server.js, finds itself in the directory and sends it over. That's a huge security risk right? Any way to solve it?
All my code files are present in the same folder; would changing that be the best way to fix the problem?

Since index.html and server.js are in the same folder then the server will send server.js when requested because it thinks it's javascript code for the client and not the server.
You should put the files you want to serve in sub folder (usually named public) like this:
public/index.html
public/style.css
then start the server using that folder with:
app.use(express.static("public"))

Related

Remove file extension from a localhost server without using .htaccess

Let's say for example I have a localhost server running on port 3000 and the url looks like: localhost:3000/index.html, how can I remove the .html extension without using .htaccess since it's on a localhost server. If there is no way without localhost then could you give me some way I could implement .htaccess in localhost? Thank you in advance
Edit: It is a site made in HTML, CSS and JS. Using a server.js file to do the backend stuff.
Most static file servers will by default implicitly serve index.html without needing to specify it in the path.
So the URL / will serve /index.html, and /somewhere will serve /somewhere/index.html.
Of course this depends on the software configuration.

Applying node.js server to a web server

I am trying to learn how to use Node.js and web sockets to create simple multi-user interactive javascript programs. I used this tutorial series by Daniel Shiffman to create this example project. My next step would be to upload it, using WinSCP, to my RaspberryPi apache2 web server, but I haven't found a way to edit the code in a way to allow for that to work, and furthermore, I do not know what piece of the programs to execute to make it function properly.
Any assistance would be great. The extent of my Node / Socket.io knowledge comes entirely from the video series mentioned above, so you can assume I know almost nothing else.
Apache is a web server and it serves your file and send them to client for you, so when you have some client side things like html site with some css, javascript and images you can use apache to send them to client for you.
In node.js you can create this web server simply by following code and express library:
// Create the app
var app = express();
// Set up the server
var server = app.listen(3000, () => {
console.log('http server is ready')
});
as you created in your code too. by this web server you can host your files and do many more things like setup socket.io server and ... because you write web server yourself. with following code you serve static files in public directory (html, css, javascript and images ...):
app.use(express.static('public'));
after you finishing this process you can run it simply by:
npm install
node server.js
if you want you can run you code inside docker by creating Dockerfile and ...
About your question, you must move all your project files into raspberry and at the end you have following directory tree in somewhere in raspberry:
|- server.js
|- package.json
\ public
at this directory run above commands and your server will be up and running and you can access to it by http://raspberry_ip:3000.

NGINX+Bolt CMS - static content calls returning 404 errors

I decided to try out Bolt CMS on my web server, which has happily run several Wordpress sites for a few months now through PHP-FPM.
The site's front end works apparently fine, but the administration section of the site has problems. Every call to static content -- JS, CSS and image files -- results in a 404 (not found) error. What's more, when I look at the NGINX error logs I see that the server tries to access the files from this location:
/usr/share/nginx/
I don't use that directory anywhere in my nginx or bolt configurations! I've done "grep -R '/usr/share' ." in my nginx and bolt configuration directories, with no results returned.
Has anyone had this problem before? where is the /usr/share/nginx/ reference coming from?
Under distributions like RHEL/CentOS (probably others), /usr/share/nginx/ is the location of the default Nginx files. You can grep -R /etc/nginx and that will point you to where.
The usual problem that I encounter (create for my own misery) is that I will create the Nginx virtual host file as something like /etc/nginx/conf.d/example.com instead of /etc/nginx/conf.d/example.com.conf (note the .conf)

Node.js /socket.io/socket.io.js not found express 4.0

So I'm trying to get chat working on my website, and when I was testing locally it worked great, because port 8080 on my localhost was available and all that good stuff. But now I pushed my code to my Heroku app, and when I try and load my chat page, I get the error stating that it can't get localhost:8080/socket.io/socket.io.js.
I've seen node.js /socket.io/socket.io.js not found
and tried the suggestions, but none worked, even moving the socket.io.js file into a resource file did not work. I'm guessing this is because I'm using express 4.0?
Any help would be appreciated
Thanks
Edit:
So to add more details, since my question could seem a little vague, here is my relevant app.js code:
var client = require('socket.io').listen(8080).sockets;
In my jade file for the chat page, I have:
script (src = `'http://localhost:8080/socket.io/socket.io.js`')
and later on
var socket = io.connect(`'http://localhost:8080`');
and all this works on localhost (I load up on port 5000, socket.io is connected to port 8080). I do this using 'foreman start' with the heroku toolbelt.
When I try and change these to work on heroku, it breaks and I'm not sure how to fix it. I hope this clarifies the question a bit.
Edit 2:
I'm running:
express 4.0.0
socket.io 0.9.16
node 0.10.x
Thanks
Do you have an explicit route in express which catches all other routes? Something like this perhaps:
app.get("/", handlers.home);
app.get("/..." ...);
...
app.get("*", handlers.error);
This might keep socket.io from being able to host it's own js file for the client. There is an easy way to fix this, since you probably already have a public or static folder setup in express. Something like:
app.use(express.static("public"));
Make a new folder called socket.io and copy over the appropriate socket.io.js file into said folder, and all should be well. However note that there are two files named socket.io.js!! So, if you see something like "Uncaught ReferenceError: require is not defined" it means you copied the "node-ey" server side file. Here is the correct client file to copy:
app_dir/node_modules/socket.io/node_modules/socket.io-client/dist/socket.io.min.js
Note #BHendricks: I would have just posted as a reply to your comment, but I currently lack the required reputation.
Edit:
The OPs question probably has more to do with the "localhost" issue. When connecting from a client (say your home IP), as far as your browser knows - localhost implies a connection with the machine which is locally hosting stuff. Since your home machine (or phone) does not host socket.io, this is failing.
What you need to do is have your server embed the socket connection information (either a fully qualified hostname, ip etc). This can be done when the server "renders" the page with the client connection.
What happens when you go to http://localhost:8080/socket.io/socket.io.js?
Does it 404? If it does you need to make sure you have it in a directory that Express is set to serve statically.
app.use(express.static(__dirname + '/public'));
Then put your socket.io.js file in public/socket.io/socket.io.js (relative to your app.js file)
Restart your server and see if that fixes it.
Basically, Express doesn't serve files statically from the file system unless you explicitly tell it where to map from.

IDEA Failed to load resource

I want to debug my html + javascript site in IDEA and Chrome browser.
When I press 'debug' in console i got:
Failed to load resource file:///C:/js/angular.min.js
As I undestand, I need to set root path of my web site in properties. But where this option?
Such urls (site root-relative, the ones starting with slash) can't be properly resolved when opening file locally (using file:// protocol) - the browser will search for these files in your system root (C:/). You need using remote javascript debug configuration (access your html using the web server url rather than local path) to make this work. See http://wiki.jetbrains.net/intellij/Debugging_JavaScript_with_IntelliJ_IDEA#Remote_debugging for more information on remote javascript debugging in Idea
Note that Idea 12 supports so-called built-in webserver (http://confluence.jetbrains.com/display/WI/built-in+web+server), so you can test your code without installing a web server.
Instead of accessing from local file system try to access your html + javascript application from a web server by putting those appication files in web server.
Install XAMPP or WAMP in your system and copy your application into the htdocs folder to run that application locally.Also edit the files using IDEA from there.So,you can parallely edit using IEDA and run using XAMPP.
http://www.apachefriends.org/f/viewtopic.php?t=14173.

Categories

Resources