Toc
  1. 一、繁琐的自建路由
  2. 二、重构路由
  3. 三、页面整体结构
Toc
0 results found
bbcfive
nodejs繁琐地自建路由
2019/01/05 后端 NodeJs

一、繁琐的自建路由

app.js

1
2
var server = require('./server');
server.startServer();

server.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
var http = require('http');
var fs = require('fs');

function startServer () {
var onRequest = function(request, response) {
if (request.url === '/' || request.url === '/home') {
response.writeHead(200, { 'Content-Type': 'text/html'});
fs.createReadStream(__dirname + '/index.html', 'utf8').pipe(response);
} else if (request.url === '/review') {
response.writeHead(200, { 'Content-Type': 'text/html'});
fs.createReadStream(__dirname + '/review.html', 'utf8').pipe(response);
} else if (request.url === '/api/v1/records') {
response.writeHead(200, { 'Content-Type': 'application/json' });
var jsonObj = {
name: 'sfafas',
job: 'coder'
}
response.end(JSON.stringify(jsonObj));
} else {
response.writeHead(200, {'Content-Type': 'text/html'});
fs.createReadStream(__dirname + '/404.html', 'utf8').pipe(response);
}
}

var server = http.createServer(onRequest);
server.listen(3000);
}

console.log('finished');
module.exports.startServer = startServer;

二、重构路由

app.js

1
2
3
4
5
6
7
8
9
10
11
12
var server = require('./server');
var router = require('./router');

var handler = require('./handler');

var handle = {};
handle['/'] = handler.home;
handle['/home'] = handler.home;
handle['/review'] = handler.review;
handle['/api/v1/records'] = handler.api_records;

server.startServer(router.route,handle);

server.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
var http = require('http');
var fs = require('fs');

function startServer (route,handle) {
var onRequest = function(request, response) {
route(handle, request.url,response);
}

var server = http.createServer(onRequest);
server.listen(3000);
}

console.log('finished');
module.exports.startServer = startServer;

handler.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
var fs = require('fs');

function home(response) {
response.writeHead(200, { 'Content-Type': 'text/html'});
fs.createReadStream(__dirname + '/index.html', 'utf8').pipe(response);
}

function review(response) {
response.writeHead(200, { 'Content-Type': 'text/html'});
fs.createReadStream(__dirname + '/review.html', 'utf8').pipe(response);
}

function api_records(response) {
response.writeHead(200, { 'Content-Type': 'application/json' });
var jsonObj = {
name: 'sfafas',
job: 'coder'
}
response.end(JSON.stringify(jsonObj));
}

module.exports = {
home: home,
review: review,
api_records: api_records
}

router.js

1
2
3
4
5
6
7
8
9
10
11
12
13
var fs = require('fs');

function route(handle, pathname,response) {
console.log('Routing a request for' + pathname);
if (typeof handle[pathname] === 'function') {
handle[pathname](response);
} else {
response.writeHead(200, {'Content-Type': 'text/html'});
fs.createReadStream(__dirname + '/404.html', 'utf8').pipe(response);
}
}

module.exports.route = route;

三、页面整体结构

avatar

本文作者:bbcfive
版权声明:本文首发于bbcfive的博客,转载请注明出处!