Created
August 16, 2015 23:19
-
-
Save giobyte8/043cf468c3bed2c6a19e to your computer and use it in GitHub Desktop.
A simple nodejs file server (Server to download files)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Simple server to download files over network | |
* | |
* @author: Giovanni Aguirre | twitter: @DiganmeGiovanni | |
* @Created on: Aug 16, 2015 | |
*/ | |
// Importa y configura los modulos necesarios | |
// para la aplicación | |
var express = require('express') | |
var app = express() | |
var http = require('http').Server(app) | |
// Ruteo de una ruta que envia JSON al cliente | |
app.get('/demofile', function(req, res) { | |
res.status = 200 | |
res.json({"hello": "world"}) | |
}) | |
// Envia al cliente un archivo de texto | |
// ubicado en la misma carpeta que este script | |
app.get('/downloadmedia', function(req, res) { | |
res.status = 200 | |
res.sendFile( __dirname + '/mediafile.txt') | |
}) | |
// Envia un archivo ubicado en una ruta arbitraria | |
// y absoluta fuera del proyecto | |
app.get('/mediafile', function(req, res) { | |
res.status = 200 | |
res.sendFile('/tmp/demomediafile.mp3') | |
}) | |
// Start application | |
http.listen(3000, function(){ | |
console.log('Download server up and running') | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment