Node.js file server with file index - javascript

I have the below code for a file server and I would like to use and html page to show an index of the files that are available for download in the static folder "public1"
I currently can serve an html page if the user explicitly requests it but I can't make it automatically serve the html page. The commented out code is my attempt at serving the html page named "hello" by default. It doesn't work...
How can I make the html page display (by default) to the user that navigates to the ip address in a browser?
How can I make the html file show an index of files in the static folder?
So for this two part question, does anyone know how to do this? Can you point me in the right direction.
var express = require('express');
var server = express();
var port = 10001;
//server.get(__dirname + 'public1', function(req, res) {
// res.send('Hello.html');
//});
server.use(express.static(__dirname + '/public1'));
server.listen(port, function() {
console.log('server listening on port ' + port);
});

The easiest way to do this is by using the serve-index and serve-static middleware that is available for express. The below example code works, just swap out the process.cwd() for whatever directory you'd like to serve.
const express = require('express');
const serveIndex = require('serve-index');
const serveStatic = require('serve-static');
const path = require('path');
const server = express();
const port = process.env.PORT || 3000;
function setHeaders(res, filepath) {
res.setHeader('Content-Disposition', 'attachment; filename=' + path.basename(filepath));
}
const dirToServe = process.cwd();
server.use('/', serveIndex(dirToServe, {icons: true}));
server.use('/', serveStatic(dirToServe, {setHeaders: setHeaders}));
server.listen(port, () => {
console.log('listening on port ' + port);
});

Related

Manipulating Cookies before serving

Scenario
I have a simple NodeJS server using the Express framework. I have some ReactJS pages that I need to serve. No Isomorphic for now.
Before serving, I want to check if the user has a particular cookie and then based on that I want to create a new cookie.
What I tried
Here is the code for my server.js file.
var express = require("express");
var app = express();
var Cookies = require("cookies");
app.use(function(req, res){
var cookies = new Cookies( req, res);
var userCookie = cookies.get("sso");
var isLoggedIn = cookies.get("isLoggedIn");
if(userCookie !== undefined && isLoggedIn === false){
cookies.set("isLoggedIn", true);
}
});
app.use(express.static(__dirname + '/build'));
var port = process.env.PORT || 8080;
app.listen(port, function() {
console.log("Listening on " + port);
});
So I am using express.static for serving static files from my build folder.
I tried moving the app.use(express.static(__dirname + '/build')); line above the previous app.use, but then the code never enters the cookies section.
I tried this simple thing. Even this didn't worked. I can see log hello, but the website isn't working.
var express = require("express");
var app = express();
app.use(function(req, res){
console.log("hello");
});
app.use(express.static(__dirname + '/build'));
var port = process.env.PORT || 8080;
app.listen(port, function() {
console.log("Listening on " + port);
});
There's a problem with your use of cookies. You're checking if isLoggedIn, the cookie value, === false, a boolean value. But cookies are always strings, so the comparison will always be false and you'll never set isLoggedIn to "true".
If your update your code to treat the values as strings it might work. Try something like this:
if(userCookie !== undefined && isLoggedIn !== "true"){
cookies.set("isLoggedIn", "true");
}
As jeremy's answer pointed out, I was using the cookies the wrong way. But that wasn't the core problem. If code would have run, express server would have thrown error. The real problem was that i wasn't calling the next middleware in the application’s request-response cycle. According to the docs
Middleware is a function with access to the request object (req), the
response object (res), and the next middleware in the application’s
request-response cycle, commonly denoted by a variable named next.
I wasn't calling the next.
So the updated running code is:
var express = require("express");
var app = express();
var Cookies = require("cookies");
app.use(function(req, res, next){
var cookies = new Cookies( req, res);
var userCookie = cookies.get("sso");
var isLoggedIn = cookies.get("isLoggedIn");
if(userCookie !== undefined && isLoggedIn !== "true"){
cookies.set("isLoggedIn", "true",{ httpOnly: false } );
}
next();
});
app.use(express.static(__dirname + '/build'));
var port = process.env.PORT || 8142;
app.listen(port, function() {
console.log("Listening on " + port);
});

How to extract zip from client in node

I'm having a node app which needs to get some zip file from client Postman and extract it to a folder in my fileSystem,Im using express I did the following which doesnt work,
what am I missing here?
I've created sample node app to simulate the issue.
var express = require('express');
var upload = require('multer')({ dest: 'uploads/' });
var admZip = require('adm-zip');
var app = express();
app.post('/',upload.single('file'),function(req,res){
debugger;
var zip = new admZip(req.file);
zip.extractAllTo("C://TestFolder//TestPathtoExtract", true);
res.send("unzip");
});
var server = app.listen(3001,function(){
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s',host,port);
})
This is how I use it im postman
If there is other way to do it with different open source this can be great!
I use
https://github.com/cthackers/adm-zip
which can be change to any other library
I've also find this lib but not sure how to use it with express
https://www.npmjs.com/package/decompress-zip
Thanks!
This is the set up I did for Postman, first this is my form-data body
Now in the header I left in blank after trying to set multipart/form-data manually and utterly failed, so no header here.
Here I did a pair of console.log, one of req.headers to be sure of Postman sending the right multipart/form-data and another of req.file
And well the output seems to be fine
Edit: the code.
var express = require('express');
var upload = require('multer')({
dest: 'uploads/'
});
var admZip = require('adm-zip');
var app = express();
app.post('/', upload.single('file'), function(req, res) {
console.log('%c > req.headers test.js [9] <=================================', 'color:blue;', req.headers);
debugger;
console.log('%c > req.file test.js [10] <=================================', 'color:blue;', req.file);
//instead of just req.file I use req.file.path as admzip needs the actual file path
var zip = new admZip(req.file.path);
zip.extractAllTo("/Users/myuser/Desktop/ext", true);
res.send("unzip");
});
var server = app.listen(3001, function() {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});
You need to pass filename as argument.
Use req.file.path
var zip = new admZip(req.file.path);

Node JS Express all requests showing 404

All requests are showing GET / 404 8.128 ms-13 in console.
I have posted the code below, there is no error in the code. I can run other NodeJS applications. But this is showing 404 in console. It is not even showing the fav icon. It worked once showing Cannot GET / error and the fav icon was visible at that time.
'use strict';
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var favicon = require('serve-favicon');
var logger = require('morgan');
var port = process.env.PORT || 8001;
var four0four = require('./utils/404')();
var environment = process.env.NODE_ENV;
app.use(favicon(__dirname + '/favicon.ico'));
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.use(logger('dev'));
app.use('/api', require('./routes'));
console.log('About to crank up node');
console.log('PORT=' + port);
console.log('NODE_ENV=' + environment);
switch (environment){
default:
console.log('** DEV **');
app.use(express.static('./src/client/'));
app.use(express.static('./'));
app.use(express.static('./tmp'));
app.use('/app/*', function(req, res, next) {
four0four.send404(req, res);
});
app.use('/*', express.static('./src/client/index.html'));
break;
}
app.listen(port, function() {
console.log('Express server listening on port ' + port);
console.log('env = ' + app.get('env') +
'\n__dirname = ' + __dirname +
'\nprocess.cwd = ' + process.cwd());
});
According to http://expressjs.com/starter/static-files.html I think that your route here app.use('/*', express.static('./src/client/index.html')); will use ./src/client/index.html as the base path and append whatever you provide to find a file. For example
/some-file will look for ./src/client/index.html/some-file which is obviously not existed
In case you want to understand it more, the static middleware use https://github.com/pillarjs/send internally to stream file
So you can do this
app.use('/*', express.static('./src/client'));
It will, by default, set / to src/client/index.html, you can change that behaviour by setting index option as specified here https://github.com/expressjs/serve-static
If you want to redirect /* to ./src/client/index.html do this
// first set the static middleware
app.use('/public', express.static('./src/client'));
// then use redirect
app.get('/*', function(req, res, next){
res.redirect('/public/index.html');
});
This setup will redirect everything to public/index.html. If you want to add APIs or other routes, put it before the app.get('/*')

Switch from node-static to express

I have a simple application running on node.js with websockets. This application uses the node-static module for serving some html pages with css, js and so on.
The folder structure is this:
-app
- index.html
- server.js
- img/
- base.png
- sub/
- sub.png
- scripts
- base.js
- sub/
- sub.js
- css
- base.css
- sub/
- sub.css
Where server.js is the server file. Inside server.js there is the following code:
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(process.env.PORT || 1234);
var WebSocketServer = require('websocket').server;
new WebSocketServer({
httpServer: app,
autoAcceptConnections: false
}).on('request', onRequest);
...
Now I need to switch from node-static to Express because I need to use routes. I used this code, however it doesn't work:
var express = require('express');
var app = express();
var http = require('http');
var httpServer = http.Server(app);
app.use(express.static(__dirname+'/app'));
app.get('/', function(req, res){
res.sendfile(__dirname + '/index.html');
});
app.listen(1234);
var WebSocketServer = require('websocket').server;
new WebSocketServer({
httpServer: app,
autoAcceptConnections: false
}).on('request', onRequest);
...
I can serve files, however it breaks the websocket connection.
What's wrong? Please note that the solution should be suitable for working on localhost and Heroku.
I resolved my problem using the following code:
var http = require("http");
var express = require("express");
var app = express();
var port = process.env.PORT || 1234;
app.use(express.static(__dirname + "/"));
var server = http.createServer(app);
server.listen(port);
console.log("http server listening on %d", port);
var WebSocketServer = require('websocket').server;
new WebSocketServer({
httpServer: server,
autoAcceptConnections: false
}).on('request', onRequest);
...
This code is derived from https://devcenter.heroku.com/articles/node-websockets, however my code use the 'websocket' node module instead of 'ws'. Thanks to #Tony for the hint.

node.js - express - URL and error handling

i'm new to node.js, so please be indulgently.
I'm just playing around with node.js and the express-module.
I had an idea how to deal with browser-requests and now i have a simple question:
Is this a good idea/practice or is there a better solution to handle that?
var http = require('http');
var express = require('express');
var fs = require('fs');
var app = express();
http.createServer(app).listen(80);
app.get('/*',function(req,res,next){
fs.exists(__dirname + req.url, function (exists) {
if(exists)
{
console.log('Sending ' + __dirname + req.url + "...");
res.sendfile(__dirname + req.url);
}
else
{
console.log(__dirname + req.url + " not found!");
res.send('Sorry, page not found.',404);
}
});
});
Express is based on Connect and as such supports its middleware. And there is a perfect middleware for your situation: static file serving.
app.use(express.static(__dirname + '/public'));
This will serve all files within the /public directory as static files available from the root directory. For routes which are not handled separately and for which no file exists, a 404 error is returned.
Btw. you want to put the listen-call to the end.

Categories

Resources