
Node and Express
So let's talk about Node, Express and Mongodb.
What is Nodejs?
Nodejs is a platform that is used to build scalable server-side applications in JavaScript.
What is Express?
Express is a micro-framework used ontop of Nodejs. Express does the following for Node:
- processing middleware
- handle HTTP verbs (GET,PUT,POST,DELETE) at different paths
- setting up the port used for connection
It is also important to note that Express is unopinionated. Meaning it has fewer restrictions on how to do things the best way. This is nice when you know all the correct tools to put together your project.
The Setup
Check if Node and npm are installed on your computer using your terminal. If they are installed the command will return a version number. If you do not have them installed follow these directions.
node -v // 10.16.0
npm -v // 6.9.0When you have Node and npm installed we are going to create a folder for the project and install express.
mkdir node-express-api //name this folder whatever you want
cd node-express-api //enter project folder Once inside the project folder run this command in the terminal to install express.
npm install express --saveCreate a file called app.js and add the code below to set up the server.
const express = require('express');
const app = express();
app.listen(3000, () => {
console.log('The application is running on localhost:3000!');
});Go back to the terminal and run node app.js to see the console log message. Your server is running.
Adding Routes
The routing methods dictate how the route will operate in the application. The breakdown of a routing method is as follows:
- app - the instance of express
- HTTP method in lowercase
- The pathway on the server
- The handler function dictates what happens when you hit that route
- The parameters of the handler function represent the API request and response.
- request - dictates which resources a client wants to interact with and how it wants to interact with those resources
- response - information that is sent back from the server. For example, a response of 500 indicates a server error. More on HTTP responses here.
- The parameters of the handler function represent the API request and response.
const express = require('express');
const app = express();
app.get('/item', (req, res) => {
//res.send sends the response body
//you can only use one res.send at a time.
res.send('A GET request was sent. GET returns all data.');
});
app.post('/item/:id', (req, res) => {
res.send('A POST request was sent. POST creates a piece data.');
});
app.put('/item/:id', (req, res) => {
res.send('A PUT request was sent. PUT updates a piece data.');
});
app.delete('/item/:id', (req, res) => {
res.send('A PUT request was sent. DELETE removes a piece data.');
});
app.listen(3000, () => {
console.log('The application is running on localhost:3000!');
});Start your server again, navigate to localhost:3000/item and you will see the response message, from the GET request, in the browser. To test the other routes you need to download a program like postman or insomnia. Once you have installed one of those applications, I'm using postman, you can test your API by toggling the type of request with the corresponding pathname(while the server is running in the terminal).
In the next post I'll do some database work with Mongodb.