问题
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.
回答1:
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}`);
}
来源:https://stackoverflow.com/questions/34559227/javascript-variables-for-get-http