Access localhost:3000 through localhost/myproject/index.html - javascript

I am new to node.js and I would like to learn if I could access the node.js port (3000) by writing the url of the index.html. I followed this tutorial to create a chat app, but I have the problem I mentioned above.
I want to be able to write localhost/myproject/index.html instead of localhost:3000 on my browser.
My server-side javascript code is this:
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
http.listen(3000, function(){
console.log('listening on *:3000');
});
Is there something I should change here? Any help would be appreciated.

If you want run your project as domain name instead of localhost:3000 then just follow this link
https://www.digitalocean.com/community/tutorials/how-to-set-up-apache-virtual-hosts-on-ubuntu-14-04-lts

To be able to access your path without defining a port you would need to use port 80 or port 443 for https.
http.listen(80, function(){
console.log('listening on *:80');
});
Concerning the path you should adjust the routing parameter, also have a look at static files in express js.
app.get('/myproject/index', function(req, res){
res.sendFile(__dirname + '/index.html');
});

Related

What do I replace "http://localhost:3000" with when using a server and not local machine?

I've been doing a lot of online courses with node and express. I want to get sockets.io to work but I can't even establish a connection at the moment. I am using a cPanel virtual private server and running code in the server terminal and then trying to use a website hosted on the server to access the .js file running on the server.
I've tried all sorts of different things but I'm reducing it to its most basic level to try get a connection. All the videos I've seen are running on a local machine and using the command prompt on a local machine to run the .js file and the browser to access http://localhost:3000.
The .js file I'm running on my cPanel server looks like this;
var express = require('express');
var app = express();
app.get('/', function(req,res){
res.send('Hello world 2');
})
app.listen(3000);
So how do I then access that via the browser? I have tried http://mywebsite.com:3000 and http://11.22.33.444:3000 if 11.22.33.444 is the server ip, but the browser just times out and there is no output in the server console.
ultimately I need to run a socket.io command that looks like this;
var socket = io.connect('http://localhost:3000');
and in all the tutorials I've seen they use this localhost:3000 but no one explains how to access this if its on an actual server so I'm pretty lost.
There are other examples like;
...
const http = require('http').createServer();
...
http.listen(3000 => () => {
console.log('listening on port 3000');
});
That's just a snippet of the code but I'm wondering how I then access that 3000 port from the browser without http://localhost:3000
IF you read the docs you will see that there is a guide how to connect it with express: https://socket.io/docs/
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
server.listen(3000);
// WARNING: app.listen(3000) will NOT work here!
app.get('/', function (req, res) {
res.status(200).json({ message: "Connected" });
});
io.on('connection', function (socket) {
console.log("somebody connected");
});
Think I just solved it. I tried a different port and it worked :/
No need to specify any address in io.connect()
const app = express();
const http = require('http').Server(app);
const io = require('socket.io')(http);
http.listen(process.env.PORT || 3000, function() {
});
<script src="/socket.io/socket.io.js"></script>
var socket = io.connect();

Can't utilize static files w/ node.js and express

I'm trying to use node.js and express to create a chat client, but as soon as I try to use external CSS or JS files, I run into GET errors.
Currently I have index.js as:
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var path = require('path');
app.use(express.static(__dirname + '/public'));
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
io.on('connection', function(socket){
socket.on('chat message', function(msg){
io.emit('chat message', msg);
});
});
http.listen(3000, function(){
console.log('listening on *:3000');
});
And in my index.html I use:
<script src="/dropdown.js"></script>
<link rel="stylesheet" type="text/css" href="/style1.css">
to link the files in the HTML.
My code structure is
index.js
index.html
public
style1.css
dropdown.js
I've looked at a bunch of other solutions on stackoverflow and none of them worked. I've tried various combinations of using express.static/app.static and various combinations of linking CSS/JS files in the html file. All of them result in GET errors.
Instead of:
app.use(express.static(__dirname + '/public'));
Try this:
app.use(express.static(path.join(process.cwd(), '/public')));
Created project with exactly same code and file structure like yours, and it worked. Make sure you have written all file names correctly and all that "obvious" stuff. And if that doesn't help can you provide some error messages?

Trying to have a simple node server point all routes at index.html

Total node newbie but would like to point all my backbone deeplinked routes at index.html so I've added this script to server.js in the route of my project, but none of the deeplinked routes seem to register my terminal and the pages consequently fail. Can anyone advise where I am going wrong?
JS - server.js
var express = require('express');
var server = express();
server.use('/public', express.static(__dirname + '/public'));
// I've also tried /*
server.get('*', function(req, res){
console.log('serve', req, res, __dirname);
res.sendFile(__dirname + '/index.html');
});
var port = 8000;
server.listen(port, function() {
console.log('server listening on port ' + port);
});
If you want to make sure that all http methods serve the index.html, you need to use all instead of get.
server.all('*', function(req, res){
console.log('serve', req, res, __dirname);
res.sendFile(__dirname + '/index.html');
});
Furthermore, make sure that you're starting the node server from within the directory where the index.html is located.

How do I serve a static file using Node Express?

I was having trouble settings up a very basic static file sever using express with Node.js. I set up a simple server.js but cannot see any files when I load the URL localhost:9000 in my web browser.
All I see is a page saying: Cannot get /
var express = require('express');
var app = express();
app.use(function(req, res, next) {
next();
});
app.use(express.static(__dirname));
app.listen(9000);
Simply you're exposing nothing. Do you have, for example, an index.html file? Try this:
app.get("/", function(req, res) {
res.sendfile("index.html");
});
Did you go through the NodeSchool workshoppers? They have step-by-step examples that cover this and more.
Here is the workshop for Express.
Here is my solution for the 'static' question in the workshop.
var express = require('express')
var app = express()
app.use(express.static(process.argv[3]||path.join(__dirname, 'public')));
app.use(require('stylus').middleware(__dirname + '/public'));
app.post('/form', function(req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' })
res.end()
})
app.listen(process.argv[2])
Express does not create a directory listing. Even thought it does not list the files in the directory, it does serve them up when hitting them in the web browser.
Point the browser to the actual file:
http://localhost:9000/public/test.html
Originally I found this confusing because I had expected the express server to list directories; when seeing "something"... a page that said "Cannot get /" I assumed that page would normally have a list of files.

express.static breaks nodejitsu application

I have no idea why this happens, but when I add a static path to my app I get an error on page of a hosting company I am using "nodejitsu" saying that application is not working, the line I am referring to is commented out in a code snippet below 'server.js' that is on the same level as my 'public' directory. I'm trying to think of a work around or other solution to define my public directory, but no luck so far, as I don't understand what could be causing an error. application uses node.js with dependencies including express and socket.io, latest versions.
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
server.listen(80);
//app.use(express.static(__dirname + '/public'));
app.get('/', function(req, res) {
res.sendFile(__dirname + '/public/index.html');
});
io.on('connection', function (socket) {
});
The express term is not defined because you didn't save it.
You will need to do something like this:
var express = require('express');
var app = express();
app.use(express.static(__dirname + '/public'));

Categories

Resources