'use strict'; var serverUrl = require('./url'); var http = require('http'); var url = require('url'); const handleScroll = (req, res) => { res.writeHead(200, {'Content-Type': 'text/html'}); res.end(''); }; const handleAPagenation = (req, res) => { let u = url.parse(req.url); let params = new url.URLSearchParams(u.search); let page = params.get('page') === null ? null : Number(params.get('page')); if (page === null || isNaN(page)) { return handle404(req, res); } let body = ''; let nextLink = u.pathname + '?page=' + (page + 1); let prevLink = u.pathname + '?page=' + (page - 1); if (page > 1) { body += 'prev | '; } body += 'next'; res.writeHead(200, {'Content-Type': 'text/html'}); res.end('' + body + ''); }; const handleLinkPagenation = (req, res) => { let u = url.parse(req.url); let params = new url.URLSearchParams(u.search); let page = params.get('page') === null ? null : Number(params.get('page')); if (page === null || isNaN(page)) { return handle404(req, res); } let head = ''; let nextLink = u.pathname + '?page=' + (page + 1); let prevLink = u.pathname + '?page=' + (page - 1); if (page > 1) { head += ''; } head += ''; res.writeHead(200, {'Content-Type': 'text/html'}); res.end('' + head + ''); }; const handleFollow = (req, res) => { let body = ''; body += 'a'; body += 'external'; body += '' res.writeHead(200, {'Content-Type': 'text/html'}); res.end('' + body + ''); } const handle404 = (req, res) => { res.writeHead(404, {'Content-Type': 'text/plain'}); res.end('not found') }; http.createServer(function (req, res) { if (req.method !== 'GET') { handle404(req, res); } let u = url.parse(req.url); if (u.pathname === '/scroll' || u.pathname === '/mark') { handleScroll(req, res); } else if (u.pathname === '/a-pagenation') { handleAPagenation(req, res); } else if (u.pathname === '/link-pagenation') { handleLinkPagenation(req, res); } else if (u.pathname === '/follow') { handleFollow(req, res); } else { handle404(req, res); } console.log(`"${req.method} ${req.url}"`, res.statusCode) }).listen(serverUrl.PORT, serverUrl.HOST);