# An introduction to express.js, frameworks, libraries and abstraction. --- ### What is express? --- - Express.js is a minimal, unopinionated web framework for node. - builds upon node's features, and offers a lot of tools that makes writing node apps MUCH easier. - Implements a lot of low level functionality behind the scenes to take care of often repeated operations of a web server(writing routes, serving static assets, cookie parsing, POST parsing etc.) --- - for example, express has a form body parser, so you can focus on writing code to actually use your form data, rather than writing code to parse it (remember streams and chunking?) - we'll learn more about express and how to use it later, but since we're now going to be using a framework, it's a good time to talk a little about frameworks and libraries in general. --- ### A segue into the difference between a framework and a library ![](https://i.imgur.com/BkjIz8k.png) --- - Both frameworks and libraries are code written by someone else that is used to help solve common problems. - but they're not interchangeable terms. --- ### Libraries ![](https://i.imgur.com/6znaXT4.png) - A library is a set of reusable functions that can be usually be plugged in wherever you want. - Libraries are written so that developers across the world dont have to rewrite the same bit of code from scratch. --- - You are in complete control of what the library does, you choose when and where to call a function from the library, it executes some code, and that ends your deal with the library. - A library is a hired gun, it comes in, does exactly what you request, and leaves. --- ### Frameworks - A framework, however, is more like a blueprint for a process. There is a control flow already in place, a system of functions that do things in a certain way, in a certain order. - The framework defines when and where you can insert your own code into this blueprint, i.e, it calls the code you plugged in, when it needs to. --- - Generally, frameworks work as one high level system, rather than a series of independent operations. - If a framework is a building contractor, **they** have direct control over the workers, and processes used to build the house. You can provide your input to shape the house to your requirements, but they do the work. --- ## Opinionated vs unopinionated frameworks - Opinionated frameworks are those with opinions about the "right way" to handle any particular task. - Unopinionated frameworks, by contrast, have far fewer restrictions on how to achieve a goal --- - Unopinionated frameworks make it easier for developers to use the optimal modules to complete a particular task, however at the cost that you need to find the modules yourself. --- - Express is unopinionated. - You can configure the behaviour between a request and a response, with a large number of compatible modules (express calls these middleware). you can also make your own middleware. - You can structure the express app in one file or multiple files, and using any directory structure. --- ## Abstraction - **People have been coding for a while.** - **If something's hard, someone's probably done it before, and shared it for free.** --- - Abstraction is the process of hiding the more complex functionality of a program, and only giving the most relevant information to the programmer. - Abstraction allows for code re-use, and libraries and frameworks are built on the idea of abstraction: they both abstract the underlying code and allow you to reuse functionality when you need it. --- ## Express vs node - an example of abstraction --- To serve static assets (css,js,image) in node, you'd write a function that looks like this: ```javascript= const handlePublic = (request, response, url) => { const extension = url.split('.')[1]; const extensionType = { html: 'text/html', css: 'text/css', js: 'application/javascript', ico: 'image/x-icon', }; // Replaced err with error in line 30 const filePath = path.join(__dirname, '..', url); fs.readFile(filePath, (error, file) => { if (error) { console.log(error); response.writeHead(404, { 'Content-Type': 'text/html' }); response.end('<h1>404 file not found</h1>'); } else { response.writeHead(200, { 'Content-Type': extensionType[extension] }); response.end(file); } }); }; ``` --- With express, you can serve a whole directory of static files (images, CSS, JavaScript etc.) from your express server with a single line of code. ```javacript= app.use(express.static('public')); ``` This will automatically handle any requests for images, css and javascript files made from your site, and points to your public folder. --- So why teach node first? **Because manually setting content-types and response http headers are fundamental to understanding how the web works.** Frameworks are nice, but you shouldn't let them be a crutch.
{"metaMigratedAt":"2023-06-15T11:54:54.403Z","metaMigratedFrom":"YAML","title":"An introduction to express.js, frameworks, libraries and abstraction.","breaks":true,"contributors":"[{\"id\":\"2967aacf-1990-431e-b963-91e79ce4a2bf\",\"add\":0,\"del\":1},{\"id\":\"3bd43981-0858-4e2b-a511-ecd7fba2d230\",\"add\":4922,\"del\":110},{\"id\":\"62face64-5436-4acc-9a7e-1c0da8ae85ba\",\"add\":403,\"del\":207}]"}
    510 views
   Owned this note