loading
open in
index.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
const SimpleHttpServer = require('./simplehttpserver.js');

const httpServer = new SimpleHttpServer(8080);

httpServer.useSetting('Routes', {
  Default: '/{controller}/{action}',
  Route1: '/{controller}/{action}/{id}',
  Route2: '/{controller}/{action}/{name}/{id}'
});

httpServer.use((env, next) => {
  //write a response to the client
  env.res.write("<p style='color:red'>Middleware1 executed</p>");
  
  //write a response to the client
  env.res.write(
    '<p>Routes coming from initial settings</p>' + JSON.stringify(env.Routes)
  );

  env.sessionKey = 'TestSession';

  next(env);
});

httpServer.use((env, next) => {
  //write a response to the client
  env.res.write("<p style='color:red'>Middleware2 executed</p>");

  //write a response to the client
  env.res.write('<p>sessionKey coming from Middleware1</p>', env.sessionKey);

  env.IsSessionValid = true;

  next(env);
});

httpServer.use((env, next) => {
  //write a response to the client
  env.res.write("<p style='color:red'>Middleware3 executed</p>");

  if (env.IsSessionValid) {
    //write a response to the client
    env.res.write('<p>Session is valid</p>');
  }

  //next(env);
});

httpServer.start();
node v9.7.1 linux/amd64