Please enable JavaScript.
Coggle requires JavaScript to display documents.
Module 2, Module 2 Overview:
This module will introduce you to the basics…
-
Module 2 Overview:
This module will introduce you to the basics of using Node.js and Express to create web apps. We'll walk through the components of Node.js and Express apps and setup a sample Hello, World! app, before you use what you've learned to set up the Node.js project for your own web app.
-
What is Node?
Node.js, we can write JavaScript code on the server-side that performs the data access and manipulation for an application before returning that data to the client.
Node.js is a server-side runtime environment that can be used to program web application servers.
Node Setup
-
Ports
In computer networking, a port is a destination or communication endpoint. When we use Node.js to set up a web server on our computers, we'll have to specify which port the Node.js app should be listening on.
NPM
The Node Package Manager (npm), comes packaged with your Node.js installation. It allows you to quickly and easily install other add-ons (frameworks and libraries) to use with your Node.js projects.
Many of these packages will be the dependencies that your project needs in order to work. A dependency is a package that your project depends on for any reason. For example, if you're using a function that you didn't write yourself that is defined as part of another framework or library, your project depends on that framework or else it won't be able to access that function.
Npm also provides an online database of public packages that allows you to access packages that other developers have created. Using external libraries, packages and frameworks makes it easier to develop code for your projects without spending time rewriting code that others have already spent time on. Instead, you can simply install a package and integrate it in your project.
Modules
Modules are collections of functions that you can utilize in your Node.js application. This allows you to easily incorporate existing code that is written for a specific scenario, which helps you focus on your app's behavior.
Note: Use single quotes (' ') instead of double quotes (" ") in your Node.js files, except for when you are writing in json
The http module
he http module is a module that is built into Node.js. It allows HTTP communication with your Node.js server.
The Package.json file
Every Node.js project contains a package.json file. This file contains metadata about your project, such as the project title, version #, author(s), etc. More importantly, it lists the dependencies that your project has- listing each dependency and the version of each dependency that your project needs
NOTE: you can run# npm init to create this file
-
Using Express With Node.js
Express is a web application framework for Node.js. Although you could use Node.js on its own, using a framework allows you to avoid spending time on the common features of web apps. Instead, you can focus on the unique features of your app, and use a framework to efficiently handle the aspects that your app has in common with most others.
Express is a framework that makes it easy to build web APIs and apps with Node.js.
Express Setup :check:
Steps to create a web API using Express with Node.js
- Import the proper modules that will help you develop your app. You'll need to import an Express module in order to use Express.
- Set up a server to listen for http requests. Express makes this step simpler than using Node.js alone.
- Configure server to receive and respond to http requests
a. Set up routes that specify how to handle different http requests.
b. Work with database to obtain data for response
c. Perform logic and algorithms, if needed, to obtain response
Routing :checkered_flag:
Routing is a fundamental aspect of developing a web API with Express. Routing refers to the process of defining how the app will respond to client requests to particular API endpoints.
An endpoint refers to a unique combination of http request type and request URI/path.
Express allows us to set up a route to handle each API endpoint. For each route, we'll provide a callback function that Express will run once a client sends a request to that endpoint.
Module 2 Summary
If you've made it to the end of this module, you should have a working web server built with Node.js and Express!
This module walked you through the basics of using Node.js and Express to build web applications. We started with the basics of setting up a Node.js project, using the command line to set up a package.json file. We learned how to include Node modules to take advantage of libraries and frameworks built by others. We used the Express module to make it easier to define routes, which let us build a Web API to handle requests from clients.
All that's missing now is persistent data. Right now, you're returning some sample responses to clients. In the next module, we'll learn all about databases so that your server can store and retrieve real data as it needs to.