Javascript variables for Get http

巧了我就是萌 提交于 2019-12-10 23:28:23

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!