Routing is basically just serving the client the content that it has asked for. In web-based client/server applications, the client specifies the desired content in the URL, which includes the path and querystring (we'll go over these parts of a URL in more detail later).
Let's take our "hello world!" example and make it a bit more interesting. We'll make a really simple website consisting of a home page, an about page, and a "not found" page. For now, we'll keep things simple and just serve plaintext instead of HTML:
var http = require('http');
http.createServer(function(req,res){
var path = req.url.replace(/\\/?(?:\\?.*)?$/,'').toLowerCase();
switch(path) {
case '':
res.writeHead(200,{'Content-type':'text/plain' });
res.end('Homepage');
break;
case '/about':
res.writeHead(200,{'Content-type':'text/plain' });
res.end('About');
break;
default:
res.writeHead(404,{'Content-type' :'text/plain'});
res.end("Not Found');
break;
}
}).listen(3000);
console.log('Server started on localhost:3000; press Ctrl-C to terminate....')$
In this example, we've created a simple routing mechanism using the http module in Node.js. We've defined three routes: the home page, the about page, and a default 404 page, and served plaintext content for each. This is just the beginning, and we can expand this mechanism to handle more complex routing scenarios and serve different types of content, like HTML, JSON, or images.
If you run this code, you'll find that you can now browse to the home page, http://localhost:3000, and the about page, http://localhost:3000/about.
Any querystrings will be ignored (so http://localhost:3000/?foo=bar will serve the home page), and any other URL (http://localhost:3000/foo) will serve the Not Found page.
- So what exactly is routing and why is it important in client/server applications?
- Routing is just serving the client the content that it has asked for. In web-based client/server applications, the client specifies the desired content in the URL, which includes the path and querystring. Routing is important in client/server applications because it allows the server to serve the client the correct content based on the client's request.
- How do you actually implement routing in web-based client/server applications?
- In web-based client/server applications, routing is implemented by parsing the path and querystring from the URL specified by the client and serving the appropriate content based on these values.
- What exactly is the http module in Node.js used for?
- The http module in Node.js provides functionality for creating and serving HTTP servers and clients.
- How does the example code demonstrate the implementation of a simple routing mechanism?
- The example code demonstrates the implementation of a simple routing mechanism by defining three routes (home page, about page, and 404 page) and serving plaintext content for each route. The http module is used to create an HTTP server that listens on port 3000 and handles incoming requests. The switch statement is used to match the requested path to one of the defined routes and serve the appropriate content.
- Can the routing mechanism be expanded to handle more complex scenarios and different types of content?
- Yes, the routing mechanism can definitely be expanded to handle more complex scenarios and different types of content. For example, different routes can be defined to serve HTML, JSON, or images, and more complex routing scenarios can be handled by using regular expressions or middleware to modify incoming requests or outgoing responses.
summary :
Routing in web-based client/server applications involves serving the client the content it has requested by parsing the path and querystring from the URL. The http module in Node.js can be used to create a simple routing mechanism, as demonstrated in the example code, which can be expanded to handle more complex scenarios and different types of content.
comment:
Writing about routing in client/server applications was an interesting experience. It's fascinating to see how a simple mechanism can be used to serve different types of content to clients based on their requests. The http module in Node.js makes it easy to create a routing mechanism, and the example code provided a good starting point for understanding how routing works.
'2023 공부한것들' 카테고리의 다른 글
[Node.js] Multer 모듈 (0) | 2023.06.22 |
---|---|
[node.js] 동기/비동기 한판 정리 (2) | 2023.06.22 |
WIL : 20230614-20230618 (0) | 2023.06.18 |
[node.js] es? (es5, es6) (0) | 2023.06.18 |
[node.js] making a simple web server with node (0) | 2023.06.18 |