How to Build a Web Server with Express JS for Node

994.0K views
•
February 22, 2019
by
Traversy Media
YouTube video player
How to Build a Web Server with Express JS for Node

TL;DR

Express is a fast, unopinionated, and minimalist web framework for Node.js, and the most popular one available. It makes building Node web apps far easier by simplifying request handling, letting you serve JSON APIs or render server-side pages. You initialize it with just a few lines, define routes, and listen on a port.

Transcript

this video is sponsored by the ultimate freelancing bundle by study web development comm which gives you everything you need to start your own freelancing business including 130 page in-depth guide invoicing and client proposal templates website templates an SEO checklist and much more visit the link in the description and use the code brad 25 to g... Read More

Key Insights

  • Express is a fast, unopinionated, and minimalist web framework for Node.js. Being unopinionated means it makes no assumptions about your design pattern, giving you full control over how you handle requests and send responses.
  • Express is a server-side or back-end framework, so it is not comparable to client-side frameworks like React, Angular, or Vue. It is often used in combination with them to build full stack apps.
  • Express is commonly used to build an API that takes requests from the front end and serves back data, usually in JSON format, which the front end then renders into client-side views.
  • Express can also render views on the server using plain HTML or a template engine like Handlebars or Pug, giving multiple options for building web apps.
  • Express makes building Node web apps about a hundred times easier than raw Node, where you must manually grab the URL, load the file, and check the content type with far more tedious code.
  • Middleware functions are functions that have access to the request and response objects, can execute any code, make changes to those objects, end the response cycle, and must call the next function in the stack.
  • The request object holds HTTP request properties like URL parameters, query strings, body data, and headers, while the response object is used to send back JSON, render templates, or redirect.
  • Other Node frameworks include Koa, Hapi.js, and Adonis. Koa and Hapi are similar to Express, while Adonis is much higher level and similar to Laravel for PHP, offering more but less freedom.

Install to Summarize YouTube Videos and Get Transcripts

Explore YouTube Video Summarizer or Get YouTube Transcript Extractor

Questions & Answers

Q: What is Express JS?

Express is a fast, unopinionated, and minimalist web framework for Node.js. Fast means it is very high-performance. Unopinionated means it is not a high-level framework like Laravel for PHP or Django for Python; it is basic at its core and does not assume you will build your app or API with any certain design pattern, giving you absolute full control over how you handle requests and respond, which also makes it minimalist.

Q: Is Express a front-end or back-end framework?

Express is a server-side or back-end framework, so it is not comparable to client-side frameworks like React, Angular, or Vue. In fact it is often used in combination with these frameworks to build powerful full stack web apps. Many times you build out the API with Express so it takes requests from the front end and serves back data, usually in JSON format, which you then render into client-side views.

Q: Why should you use Express instead of raw Node.js?

Express makes building Node web apps about a hundred times easier. In a raw Node crash course, building a simple web server required quite a bit of code just to do something very simple, such as manually grabbing the URL, loading the file, and checking the content type, which was very tedious. Express makes handling requests very simple and requires much less code, working for both JSON APIs and server-rendered back-end applications.

Q: What is middleware in Express?

Middleware functions are functions that have access to the request and response objects, just like route handlers do. Express has its own middleware, third-party packages provide middleware, and you can write your own. Middleware can execute any code, make changes to the request and response objects, and end the response cycle. When you write middleware you call a next function to pass control to the next middleware in the stack, so it acts as a stack of functions that runs whenever a request is made.

Q: What are the request and response objects in Express?

The request object represents the HTTP request and includes properties like URL parameters, query strings, any data sent within the body, and the HTTP headers. Middleware can change or add things to this object. The response object represents the HTTP response, and it is up to you to use it to send back JSON data, render a template, or redirect using something like res.redirect. Both objects are central to how Express handles communication with the client.

Q: What should you know before learning Express?

At the very least you should be comfortable with JavaScript. It also helps to have at least basic knowledge of Node and NPM, the node package manager; you do not need to be a Node pro but should watch a couple of tutorials and understand the core modules. Knowing HTTP and status codes helps since you deal with requests and responses, and familiarity with newer JavaScript features like high-order array methods, arrow functions, and promises is recommended.

Q: How do you create a basic Express web server?

You bring in Express at the top using CommonJS require syntax, then initialize a variable, usually called app, by calling the express method. You create endpoints, such as accepting a get request to the index route (the slash), with a callback function that takes a request and a response and does whatever you want, like responding with hello world. Finally you listen on a port, such as 5000, so visiting localhost on that port shows your output.

Q: What alternatives to Express exist for Node?

Other Node frameworks include Koa, Hapi.js, and Adonis, which are all great but nowhere near as popular as Express, which is by far the most popular framework. Koa and Hapi.js are very similar to Express. Adonis is much higher level; in the presenter's opinion it is similar to Laravel for PHP and gives you a lot more, but you do not have as much freedom and it is a much larger framework.

Q: What tools are used to follow along with this Express course?

You need Node.js installed, available from nodejs.org. The course uses Express version 4.16.4, noting version 5 was in alpha at the time. Postman, an HTTP client from getpostman.com, is used to make get, put, post, and delete requests to the server. The text editor is VS Code, using its integrated terminal for installing packages. You start a project by running npm init with the -y flag and then npm install express.

Summary & Key Takeaways

  • Express is the most popular web framework for Node.js, described as fast, unopinionated, and minimalist. Because it is unopinionated it does not assume any design pattern, letting developers keep full control over how requests are handled and how responses are sent back to the client.

  • As a back-end framework, Express is often paired with client-side frameworks like React, Angular, or Vue. Developers typically build an API with Express that serves JSON data to the front end, though Express can also render server-side views using plain HTML or template engines like Handlebars or Pug.

  • A basic Express server requires bringing in Express with CommonJS require syntax, initializing an app variable, defining routes with callbacks that take request and response objects, and listening on a port such as 5000. Middleware, the router, and body parser extend this foundation for larger applications.


Read in Other Languages (beta)

Share This Summary 📚

Summarize YouTube Videos and Get Video Transcripts with 1-Click

Download browser extensions on:

Try YouTube Summary with ChatGPT & Claude or YouTube Transcript Generator

Explore More Summaries from Traversy Media 📚

Summarize YouTube Videos and Get Video Transcripts with 1-Click

Download browser extensions on:

Try YouTube Summary with ChatGPT & Claude or YouTube Transcript Generator