nodejs
--routing
---- dynamic
---- nopage
---- static
---- main
---- index.js
// server.js // . const http = require('http'); const routing = require('./routing'); let server = new http.Server(function(req, res) { // API POST- JSON, // jsonString var jsonString = ''; res.setHeader('Content-Type', 'application/json'); req.on('data', (data) => { // - . jsonString += data; }); req.on('end', () => {// - . routing.define(req, res, jsonString); // define . }); }); server.listen(8000, 'localhost');
// /routing/index.js const define = function(req, res, postData) { res.end('Hello, Habrahabr!'); } exports.define = define;
node server.js
// /routing/index.js // . const url = require('url'); const fs = require('fs'); const define = function(req, res, postData) { // . localhost:3000/test, path '/test' const urlParsed = url.parse(req.url, true); let path = urlParsed.pathname; // server.js. , // systemd, , , /etc/systemd/system/... prePath = __dirname; try { // . // localhost:8000/api, /routing/dynamic/api, , // index.js, . , try/catch , // fs.readFile, , // . let dynPath = './dynamic/' + path; let routeDestination = require(dynPath); res.end('We have API!'); } catch (err) { // api? . res.end("We don't have API!"); } }; exports.define = define;
/routing/dynamic/api
, and test what we have. I will use for this purpose my ready-made script at / dm / shortenUrl./routing/nopage
and create index.html
there. Just create the backbone of the html page, and make a single h1 heading with the text: "404". After that, go back to /routing/index.js
, but now we will focus on the catch block we’ve already written: // /routing/index.js: catch catch (err) { // . // , '=>', es6, // . let filePath = prePath+'/static'+path+'/index.html'; fs.readFile(filePath, 'utf-8', (err, html) => { // , 404 . // — , -. if(err) { let nopath = '/var/www/html/nodejs/routing/nopage/index.html'; fs.readFile(nopath, (err , html) => { if(!err) { res.writeHead(404, {'Content-Type': 'text/html'}); res.end(html); } // - , , // - , . else{ let text = "Something went wrong. Please contact webmaster@forgetable.ru"; res.writeHead(404, {'Content-Type': 'text/plain'}); res.end(text); } }); } else{ // , , . res.writeHead(200, {'Content-Type': 'text/html'}); res.end(html); } }); }
/routing/static
. In my case, page 404 looks like this: let dynPath = './dynamic/' + path; let routeDestination = require(dynPath); routeDestination.promise(res,postData,req).then( result => { res.writeHead(200); res.end(result); return; }, error => { let endMessage = {}; endMessage.error = 1; endMessage.errorName = error; res.end(JSON.stringify(endMessage)); return; } );
/routing/static/somepage
, which works great, for example, on Apache, you will encounter some problems./routing/index.js
. Now we will write the code right before try / catch: // path prePath. , // . , // , : style.css, test.js, song.mp3 if(/\./.test(path)) { if(path == 'favicon.ico') { // - , 'favicon.ico' // , prePath, : '/var/www/html/nodejs/routing/favicon.ico'. // return, . let readStream = fs.createReadStream(prePath+path); readStream.pipe(res); return; } else{ // , , , // res.head, , , . // css, js mp3 , // , , , . if(/\.mp3$/gi.test(path)) { res.writeHead(200, { 'Content-Type': 'audio/mpeg' }); } else if(/\.css$/gi.test(path)) { res.writeHead(200, { 'Content-Type': 'text/css' }); } else if(/\.js$/gi.test(path)) { res.writeHead(200, { 'Content-Type': 'application/javascript' }); } // -, return, . let readStream = fs.createReadStream(prePath+path); readStream.pipe(res); return; } }
Source: https://habr.com/ru/post/327440/
All Articles