2

I am learning JavaScript and would like to do the JavaScript equivalent of PHP's $_GET[Var] = $foo; I am coding a basic CDN type server for a project, also, how can I serve a file for download with JavaScript? The plan is to run this code inside a NodeJS node. Sorry if I explained this badly, I am terrible at explaining things.

Adam Azad
  • 11,171
  • 5
  • 29
  • 70
alex tix
  • 175
  • 2
  • 7
  • Possible duplicate of [How to get GET (query string) variables in Express.js on Node.js?](http://stackoverflow.com/questions/6912584/how-to-get-get-query-string-variables-in-express-js-on-node-js) – Venkat.R Jan 03 '16 at 01:22

1 Answers1

0

To serve existing files from your Node.js app, use express with express.static: http://expressjs.com/en/starter/static-files.html

Example below uses ECMAScript 2015 elements and assumes Node.js 4 or 5 with static files stored in public directory:

const http = require('http'); 
const express = require('express');

const app = express();
app.use(express.static('public'));

const server = http.createServer(app).listen(8080, serverCallback);

function serverCallback() {
  const host = server.address().address;
  const port = server.address().port;
  console.log(`Server listening on ${host}:${port}`);
} 
krl
  • 5,087
  • 4
  • 36
  • 53