Express / Node / Cloud9 - issues serving static files - node.js

I'm having an issue serving a simple static html file with Express on a Cloud9 server.
My file structure is simply:
/workspace
/public
Screenshot from c9.io here
And here's my code, I get a Cannot GET/ (screenshot) when previewing in the browser.
static.js (located in the C9 workspace):
var http = require('http');
var path = require('path');
var express = require('express');
var app = express();
app.use(express.static(path.join(__dirname, '/public')));
http.createServer(app).listen(8080);
static.html (located in /public):
<!doctype html>
<html>
<body>
<p>Hello World!</p>
</body>
</html>
I know the server's listening on the right port, because the preview works when I write directly to the response stream using Express, like this:
app.use(function(request, response) {
response.end("Hello world!\n");
});
http.createServer(app).listen(8080);
Apologies for the simplicity of the request, I just pared everything down to understand the fundamental reason I'm seeing "Cannot GET/".
Thanks for your help.

renaming the file from static.html to index.html will work

Related

How to send static files (CSS, js) with nodejs express on AWS Bitnami server?

I'm developing a MEAN app using bitnami through AWS but this is the first time I've done this. I can't figure out how to send the static files in the public folder such as css and js.
I feel like I've tried every combination of configuring the apache proxy and including or not including "public" in the path but I must be missing something fundamentally. I've looked at the other Q&A on this topic but I think what I tried below uses those methods. Don't know if it has something to do with the proxy since I'm forwarding http://public-ip/test3 to the node server.
The bitnami-apps-prefix.conf file:
ProxyPass /test3 http://127.0.0.1:3100/
ProxyPassReverse /test3 http://127.0.0.1:3100/
The nodejs app.js file - tried it with an withough public in various places
var express = require("express");
const path = require('path');
var app = express();
app.use(express.static(path.join(__dirname, '/public')));
app.get("/", function(req,res){
res.sendFile(__dirname+"/public/p4.1.html");
});
app.listen(3100,'localhost',function(){
console.log("server has started");
});
the p4.1.html file - I've tried it with and without public in the path.
<html><head>
<link rel="stylesheet" href="public/styles.css">
<script src="public/functions.js"></script>
</head>
The p4.1.html, styles.css, and functions.js are stored in the public folder. The public folder is at the same path as the app.js file.
The html page does display but I get 404 errors for the css and js: http://public-ip/public/functions.js net::ERR_ABORTED 404 (Not Found)
Is this something in my code or in the proxy perhaps? Any help is appreciated.
So the solution was to put test3 instead of public in the url in the html file.
<html><head>
<link rel="stylesheet" href="test3/styles.css">
<script src="test3/functions.js"></script>
</head>

Socket.io how create file run time on local server

I have written down this code in NPM module with the help of socket.io,
Index.html
<html> <head> <title>WebRTC client</title> </head> <body>
<script src='socket.io/socket.io.js'></script> </body> </html>
In server.js file
var static = require('node-static');
var http = require('http');
var file = new(static.Server)();
var app = http.createServer(function (req, res) {
file.serve(req, res);
}).listen(8181);
var io = require('socket.io').listen(app);
io.sockets.on('connection', function (socket){
console.log('io.sockets.on');
});
Root folder has index.html, server.js, and socket.io folder contains no file
Hit localhost:8181 in a browser, index.html will run and socket.io/socket.io.js file automatically created
http://localhost:8181/socket.io/socket.io.js
and I checked my socket.io folder there is no file? How socket.io.js created? and what is the main purpose of a socket.io/socket.io.js file?
"You might be wondering where the /socket.io/socket.io.js file comes from, since we neither add it and nor does it exist on the filesystem. This is part of the magic done by io.listen on the server. It creates a handler on the server to serve the socket.io.js script file."
from the book Socket.IO Real-time Web Application Development, page 56

unabe to get static css files at express.js

i am trying by first express web app and I am using ejs template to render my web page and css to style my pages.
but server is unable to render css file from public folder. i put the static file path at express .js as follow
express = require('express'),
var app = express();
i tried to put static file path to express.js
// app.use(express.static('../public'));
app.use("/public",express.static(__dirname + '/public'));
// app.use(express.static('../../public')); //./
// app.use('/', express.static(__dirname + '/public'));
//app.use(express.static(__dirname + '../../public'));
and gave link to to my signup.ejs page
<link href="/public/mystyle.css" rel="stylesheet" type="text/css">
my directory structure as follow
app
|-view
|- signup.ejs
public
|-mystyle.css
express.js
When i started my server its give me 404 not found error. although my localhost is working good only problem is my web page is unable to get css file in webpage
Are you sure the port you have entered in the app.listen() is correct?
try
app.listen(port no., 'your localhost', function() {
console.log("... port %d in %s mode", app.address().port, app.settings.env);
});
Change the following two things:
Make link relative: app.use("/public",express.static(__dirname + './public'));
No need of navigating to /public again, since express.js already searches for static files in this folder: <link href="/mystyle.css" rel="stylesheet" type="text/css">
See: http://expressjs.com/en/starter/static-files.html

Find a css file node js

Hi I'm new to nodejs and I've just succeed to deploy a nodejs app online. Now I would like to know how to link CSS, JS or image files be cause when I try to do it like I used to, I get the error GET (not found).
The folder architecture is:
--public
--assets
--css
index.css
--js
--views
--index.ejs
--node modules
app.js
package.json
Assuming that I want to code the link index.css in index.ejs, what I need to write in my app.js file please.
app.js code:
var express = require('express');
var app = express.createServer();
app.set('view engine', 'ejs');
app.use(express.static(__dirname + '/public'));
app.get('/', function(req,res){
res.render('index');
});
app.listen(8080,'IP_ADRESS');
index.ejs code:
<link rel="stylesheet" type="text/css" href="/css/index.css">
The basic set up of serving static files in express :
var express = require("express");
var app = express();
...
app.use(express.static(__dirname + '/public'));
...
app.listen(3000);
Now in your .ejs in order to load the css styles or js scripts :
<link rel="stylesheet" href="/css/index.css"/>
First, there's a thing called a static file. Basically, that's a file that's sent over the internet without any kind of modification. Image and CSS files are typically static files.
It looks like you've put your static files in a folder called public.
Express has a built-in feature for sending static files, called express.static. You can use it like this:
// Require the modules we need.
var express = require('express');
var path = require('path');
// Create an Express app.
var app = express();
// Get the path to the `public` folder.
// __dirname is the folder that `app.js` is in.
var publicPath = path.resolve(__dirname, 'public');
// Serve this path with the Express static file middleware.
app.use(express.static(publicPath));
// ...
You should now be able to see static files. If your site is normally accessible at http://localhost:3000, you'll be able to see index.css at http://localhost:3000/css/index.css.
If you want to know way too much about express.static, you can check out a blog post I wrote that goes into Express's static files in depth.

Does Socket.io work ? Been trying to make it work for 2 weeks now

I am new to socket.io. It sounds great but nothing works for me.
I have two file listed below: app.js and index.html. I have them both in the same directory. I cd to that directory and run:
node app.js
and then I go to my browser and visit http://hostname:8888 and I get:
Cannot GET /
so I try http://hostname:8888/index.html and get:
Cannot GET /index.html
Why is it not working and why am I getting
npm ERR! invalid: socket.io-client#0.9.11
Please someone help. Thank you.
// app.js
var express = require('express')
, http = require('http');
var app = express();
var server = http.createServer(app);
var io = require('socket.io').listen(server);
app.configure(function() {
app.use(express.static(__dirname + '/public'));
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
server.listen(8888);
// index.html
<!DOCTYPE html>
<html lang=en-ca>
<head>
<meta charset=utf-8>
<title>To Do List</title>
</head>
<body>
<script src="http://localhost:8888/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost:8888');
</script>
</body>
</html>
npm ERR! invalid: socket.io-client#0.9.11 /home/username/node_modules/socket.io-client
npm ERR! not ok code 0
You write about your app.js and index.html:
I have them both in the same directory.
But your code says:
app.use(express.static(__dirname + '/public'));
Which basically means: "my index.html file should be found in a directory named public which can be found in the same directory as app.js". So it's expecting this directory structure:
directory/app.js
directory/public/index.html
So either create an public directory and move your index.html there, or tell express.static to find it in the same directory as app.js like so:
app.use(express.static(__dirname));
(Although, as #josh3736 points out in the comments, that introduces security issues so you don't want to use that solution in any production code! For testing purposes though, it's okay.)
As for your other problem, it's unclear what's causing that error. Please add a little more npm output.
However, you don't need to install the socket.io-client package if you're going to run the socket.io client from your browser, since it's already part of the socket.io package (the -client package is meant to be used to implement a client from Node itself).

Resources