Skip to content

Instantly share code, notes, and snippets.

@YuriiSerdiuk
Created April 4, 2018 08:27
Show Gist options
  • Save YuriiSerdiuk/9dc3836595113ac9b9f063f60eb09dfe to your computer and use it in GitHub Desktop.
Save YuriiSerdiuk/9dc3836595113ac9b9f063f60eb09dfe to your computer and use it in GitHub Desktop.
const http = require('http');
const https = require('https');
const fs = require('fs');
const {JSDOM} = require('jsdom');
const PORT =8000;
const urlMap = {
'/img/' : ()=> {
return fs.readFileSync('./index.html');
},
'/users/' : ()=> 'user : admin \n user : Vasia',
'/posts/':()=>{}
};
const getRaw = (index2) => {
return new Promise((resolve, reject) => {
let raw = '';
https.get(index2, (res) => {
res.on('data', (chunk) => {
raw += chunk;
});
res.on("end", () => {
resolve(raw);
})
})
}).then((raw)=>{
console.log(raw);
});
};
const server = http.createServer((request,response) => {
let ind = request.url.split('=');
let index2 = ind[1];
getRaw(index2);
debugger;
const url = request.url;
if (url in urlMap ){
response.write(urlMap[url]());
}else {
response.write('404 not found link');
}
response.end();
} ).listen(PORT);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment