| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
In this project, we will create a local node chat server. After building out the server, we'll use postman unit tests to make sure the API has been built out correctly and also use a pre-built front-end to interact with the API.
In this step, we will create a package.json and install our dependencies.
Using npm we can quickly create a package.json file. In your terminal, when in the root of the project, run npm init -y. The -y flag will create a package.json file with all the default values. It's that simple. Now we can use npm to install packages and save them to the package.json file using the --save flag. Run npm install --save express body-parser to install and save both packages.
In this step, we will create a .gitignore file so our node_modules folder doesn't get tracked.
node_modules
In this step, we will create our index.js file.
To begin let's open server/index.js and require the packages our server will need. Express is a minimalist web framework for Node that will allow us to spin up a server in no-time. We can create an express application by requiring it and saving express() to a variable. Let's create a variable called app that equals express().
const express = require('express');
const bodyParser = require('body-parser');
const app = express();We now have a full express application stored in app. If you were to console log app you would see it's a large object with many methods we can make use of. One of them we'll use is called listen. This will allow us to say what port the server should listen on. Let's have our server listen on port 3000.
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const port = 3000;
app.listen( port, () => { console.log(`Server listening on port ${port}`); } );Why did you use a port variable? This variable is not required. However, say for some reason you needed to change the port, you now only have to change it in one place instead of two.
We now have an express server listening for requests on port 3000 and when we start up the server we'll see the console log of Server listening on port 3000.
server/index.jsconst express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use( bodyParser.json() );
const port = 3000;
app.listen( port, () => { console.log(`Server listening on port ${port}.`); } );In this step, we will create a controller that will handle the logic to create, read, update, and delete messages. A message will be an object with an id, text, and time property.
Now that we have a server listening for requests, let's create a controller that will execute logic when certain requests come in. Create a controllers folder in server/ and a messages_controller.js file in server/controllers. Inside that file let's create an array that will keep track of all the messages. We'll also need a variable that will keep track of what ID to assign to new messages to keep them unique. Let's create an id variable that is equal to 0.
let messages = [];
let id = 0;Now let's use module.exports to export an object. We'll put all our methods on this object. We are using module.exports so that we can import the controller into index.js to setup routes. I'll go into more detail when that time comes. For now, let's make a create, read, update, and delete method. Each method should be a function that has two parameters, one called req and one called res.
let messages = [];
let id = 0;
module.exports = {
create: ( req, res ) => {
},
read: ( req, res ) => {
},
update: ( req, res ) => {
},
delete: ( req, res ) => {
}
}The create method should create a new message object using text and time from the request body and also the global id variable. It should then push this new messsage object into the messages array. After a new message object is created, id should be incremented by one so that the previous id won't be used on any other future messages. This will effectively keep the id unique for every message. We'll then want to send the updated messages array.
create: ( req, res ) => {
const { text, time } = req.body;
messages.push({ id, text, time });
id++;
res.status(200).send( messages );
}The read method should return the entire messages array.
read: ( req, res ) => {
res.status(200).send( messages );
}The update method should update the text property of a message using the text value from the request body. It should also determine which message to update based on the value of id from the request url parameters. We can use .findIndex to get the index where the ids match. We can then get the object using the index and update the object. Then we can return the updated messages array.
update: ( req, res ) => {
const { text } = req.body;
const updateID = req.params.id;
const messageIndex = messages.findIndex( message => message.id == updateID );
let message = messages[ messageIndex ];
messages[ messageIndex ] = {
id: message.id,
text: text || message.text,
time: message.time
};
res.status(200).send( messages );
}The delete method should delete a message using the value of id from the request url parameters. We can use .findIndex again with the id to get the index of the message object and then use .splice to remove it from the messages array. We'll then want to send the updated messages array.
delete: ( req, res ) => {
const deleteID = req.params.id;
messageIndex = messages.findIndex( message => message.id == deleteID );
messages.splice(messageIndex, 1);
res.status(200).send( messages );
}We now have all the logic we need to create, read, update, and delete messages. Now we can import our controller into index.js and create routes that will execute the logic.
server/controllers/messages_controller.jslet messages = [];
let id = 0;
module.exports = {
create: ( req, res ) => {
const { text, time } = req.body;
messages.push({ id, text, time });
id++;
res.status(200).send( messages );
},
read: ( req, res ) => {
res.status(200).send( messages );
},
update: ( req, res ) => {
const { text } = req.body;
const updateID = req.params.id;
const messageIndex = messages.findIndex( message => message.id == updateID );
let message = messages[ messageIndex ];
messages[ messageIndex ] = {
id: message.id,
text: text || message.text,
time: message.time
};
res.status(200).send( messages );
},
delete: ( req, res ) => {
const deleteID = req.params.id;
messageIndex = messages.findIndex( message => message.id == deleteID );
messages.splice(messageIndex, 1);
res.status(200).send( messages );
}
};In this step, we will hook up our controller to our app in server/index.js.
Let's begin by opening server/index.js. Since we used module.exports in our server/controllers/messages_controller.js we can require it in our index.js. The entire index.js will have access to all the methods we put on the object ( create, read, update, and delete ).
const mc = require( __dirname + '/controllers/messages_controller');We can then use the built-in methods express gives us to create endpoints. We'll use post for create; get for read; put for update; and delete for delete. We'll also make a messagesBaseUrl variable so that if the URL ever changes we won't have to update in four different places. The messagesBaseUrl should equal /api/messages.
const messagesBaseUrl = "/api/messages";
app.post( messagesBaseUrl, mc.create );
app.get( messagesBaseUrl, mc.read );
app.put( messagesBaseUrl, mc.update );
app.delete( messagesBaseUrl, mc.delete );For the put and delete endpoints, we need to add on a url parameter of id. A url paramter can be defined by adding :variableName when making the URL for an endpoint.
const messagesBaseUrl = "/api/messages";
app.post( messagesBaseUrl, mc.create );
app.get( messagesBaseUrl, mc.read );
app.put( `${messagesBaseUrl}/:id`, mc.update );
app.delete( `${messagesBaseUrl}/:id`, mc.delete );Now when a get request is sent to http://localhost:3000 our read function will be executed in our messages_controller. Which will then send a response of the messages array. Here is a map of what happens when certain requests come through:
const express = require('express');
const bodyParser = require('body-parser');
const mc = require( __dirname + '/controllers/messages_controller');
const app = express();
app.use( bodyParser.json() );
const messagesBaseUrl = "/api/messages";
app.post( messagesBaseUrl, mc.create );
app.get( messagesBaseUrl, mc.read );
app.put( `${messagesBaseUrl}/:id`, mc.update );
app.delete( `${messagesBaseUrl}/:id`, mc.delete );
const port = 3000;
app.listen( port, () => { console.log(`Server listening on port ${port}.`); } );In this step, we will test the API endpoints using postman unit tests.
In this step, we will setup the API to serve our front-end files.
const express = require('express');
const bodyParser = require('body-parser');
const mc = require( __dirname + '/controllers/messages_controller');
const app = express();
app.use( bodyParser.json() );
app.use( express.static( __dirname + '/../public/build' ) );
const baseURL = "/api/messages";
app.post( baseURL, mc.create );
app.get( baseURL, mc.read );
app.put( `${baseURL}/:id`, mc.update );
app.delete( `${baseURL}/:id`, mc.delete );
const port = 3000;
app.listen( port, () => { console.log(`Server listening on port ${port}.`); } );If you see a problem or a typo, please fork, make the necessary changes, and create a pull request so we can review your changes and merge them into the master repo and branch.
© DevMountain LLC, 2017. Unauthorized use and/or duplication of this material without express and written permission from DevMountain, LLC is strictly prohibited. Excerpts and links may be used, provided that full and clear credit is given to DevMountain with appropriate and specific direction to the original content.
| Back | FazBrowse Home | New Git URL |