관리 메뉴

Silver Library (Archived)

nodejs EP 4 - middleware 본문

Personal DB/Unclassified record

nodejs EP 4 - middleware

Ayin Kim 2021. 2. 19. 12:21
반응형

What is 'middleware' express?

Something there goes connection until it goes finishes(executed).

Between the user and last response. Something is in in that middle of it.

 

In express, every function can become middleware.

 

const betweenHome = () => console.log("I'm between");

 

If you turn on the page you input that const variable, now you can see the loading circle is spinning forever.

 

That's how middleware works.

Let user(?) to stay in the middle of in between user's home(/) request and handleHome.

[note*: this might be useful to take simultanous data to receive and display]

[e.g. to check whether user is logged in or not.]

 

To fix this, let browser to have permisson to keep executing the incoming request.

[*All the routes in express that handles connection have three: request, response, next]

 

By adding 'next' key at the const variable that designated as middleware,

now you are ready to return the route(e.g. app.use).

 

In a nutshell, this is the purpose of middleware. Imagine how plumber works - they need light. So it is.

azure.microsoft.com/ko-kr/overview/what-is-middleware/

 

미들웨어란? - 정의 및 예 | Microsoft Azure

미들웨어는 운영 체제와 해당 운영 체제에서 실행되는 응용 프로그램 사이에 존재하는 소프트웨어로, 통신 및 데이터 관리를 가능하게 합니다.

azure.microsoft.com


 

 

 

What is 'Morgan'? One of middleware. (See npmjs.com for more available one)

 

after importing morgan, let code

 

app.use(morgan("tiny"));

 

This tiny is one of morgan's preset command. For instance, combined will let Terminal showing 'what kind of browser, type of accessing' will be displayed on developer side. 'Tiny' will start logging the activity of that specific user's activity(trace).

 

As expected, this kind of middleware can be used to enhance the web security.

'Helmet' middleware is the one.

 

app.use(helmet());

 

Note: 'no need to add any others except that code. It let code to enhance security.'

In a nutshell, morgan records the log.


To intercept some specific activity of user, there is somewhat 'return' like from Python does exists here.

 

 

const middleware = (req, res, next) => {

    res.send("not happening");

};

 

As you can see, app.get("/", middleware, handleHome)'s middleware key is the one suppose to receive.

But const middleware intercepted and decide to respond as "Not happening" message earlier.

 

Hence, go 1) /profile => 2) back to port address (e.g. localhost:4000)

 

Now it shows "not happening".

Remember, to prove its activation of this: const variable for middleware purpose and 'app.get' route should include that same const  as the key to responding of it.

 

Caution: connection can only be terminated by middleware IF const variable (of middleware purpose) and its following sub-code follows with the function to execute (see the picture above) 'res.send' instead of function next.

 

Note!

The app.get() function routes the HTTP GET Requests to the path which is being specified with the specified callback functions. Basically is it intended for binding the middleware to your application.

www.geeksforgeeks.org/express-js-app-get-request-function/

 


Cookie parser, body parser = belongs to the 'express' 's middleware. The name represents where to support of.

 

What body-parser do?

Allow developer(me) to access to the request object, which sent by user

(can be any form. Like id, password. But not a plain form for security reason).

 

What can I do? To be able to access the info from body.

 

[Caution] This body-parser need configuration. Such as 'which type of file will browser to read? html, json, etc. If html, then let browser to understand the server is urlencoded. That's the configuration and that's how configuring the server, and how server understands data coming from the user, and how to understnad cookie from user.

 

 

- For config of body-parser, refer his github's bodyParser.urlencoded section.

To review how 'connection' to be reached to the route from the beginning, strongly recommended to recap 'routing def'.

www.geeksforgeeks.org/routing-in-node-js/

 

Routing in Node.js - GeeksforGeeks

A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

www.geeksforgeeks.org

Okay, what about cookie parser?

Save user info in the cookie to treat session (yes, probably login session is expired's that session!).