| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
In this project, we will create a node server that will act as a bookshelf. This server will keep track of books by being able to add books to a collection, read from the collection, update the collection, and delete from the collection. We'll use postman to test our endpoints.
In this step, we will create a package.json to keep track of our server's packages.
In this step, we will install the most common packages you'll use when making a node server.
In this step, we will create a .gitignore file to ignore the node_modules folder npm install created.
node_modules
In this step, we will create our server and have it listen on port 4000.
const express = require('express');
const app = express();
app.use(express.json());
const port = 4000;
app.listen(port, () => {
console.log(`Server listening on port ${port}`);
});In this step, we will create a controller that keeps track of the book collection and import that controller into server/index.js.
let books = [];
let id = 0;
module.exports = {};const express = require("express");
const bc = require("./controllers/books_controller.js");
const app = express();
app.use(express.json());
const port = 4000;
app.listen(port, () => {
console.log(`Server listening on port ${port}`);
});In this step, we will create the endpoint to get all the books. We will also create the method in the books_controller to be used as the endpoint's handler function.
When creating a get route, you can use the get method on the app object. The first argument is the URL of the request and the second argument is the function to execute when that URL is hit. We will be getting that function from the books_controller.
let books = [];
let id = 0;
module.exports = {
read: (req, res) => {
res.status(200).send(books);
}
};const express = require("express");
const bc = require("./controllers/books_controller.js");
const app = express();
app.use(express.json());
app.get("/api/books", bc.read);
const port = 4000;
app.listen(port, () => {
console.log(`Server listening on port ${port}`);
});In this step, we will create the endpoint to post a new book. We will also create the method in the books_controller to be used as the endpoint's handler function.
let books = [];
let id = 0;
module.exports = {
read: (req, res) => {
res.status(200).send(books);
},
create: (req, res) => {
const { title, author } = req.body;
let book = {
id: id,
title: title,
author: author
}
books.push(book);
id++;
res.status(200).send(books);
}
};const express = require("express");
const bc = require("./controllers/books_controller.js");
const app = express();
app.use(express.json());
app.get("/api/books", bc.read);
app.post("/api/books", bc.create);
const port = 4000;
app.listen(port, () => {
console.log(`Server listening on port ${port}`);
});In this step, we will create a put endpoint to update a specific book by it's id. We will also create the method in the books_controller to be used as the endpoint's handler function.
let books = [];
let id = 0;
module.exports = {
read: (req, res) => {
res.status(200).send(books);
},
create: (req, res) => {
const { title, author } = req.body;
let book = {
id: id,
title: title,
author: author
};
books.push(book);
id++;
res.status(200).send(books);
},
update: (req, res) => {
let index = null;
books.forEach((book, i) => {
if (book.id === Number(req.params.id)) index = i;
});
books[index] = {
id: books[index].id,
title: req.body.title || books[index].title,
author: req.body.author || books[index].author
};
res.status(200).send(books);
}
};const express = require("express");
const bc = require("./controllers/books_controller.js");
const app = express();
app.use(express.json());
app.get("/api/books", bc.read);
app.post("/api/books", bc.create);
app.put("/api/books/:id", bc.update);
const port = 4000;
app.listen(port, () => {
console.log(`Server listening on port ${port}`);
});In this step, we will create a delete endpoint to delete a specific book by it's id. We will also create the method in the books_controller to be used as the endpoint's handler function.
let books = [];
let id = 0;
module.exports = {
read: (req, res) => {
res.status(200).send(books);
},
create: (req, res) => {
const { title, author } = req.body;
let book = {
id: id,
title: title,
author: author
};
books.push(book);
id++;
res.status(200).send(books);
},
update: (req, res) => {
let index = null;
books.forEach((book, i) => {
if (book.id === Number(req.params.id)) index = i;
});
books[index] = {
id: books[index].id,
title: req.body.title || books[index].title,
author: req.body.author || books[index].author
};
res.status(200).send(books);
},
delete: (req, res) => {
let index = null;
books.forEach((book, i) => {
if (book.id === Number(req.params.id)) index = i;
});
books.splice(index, 1);
res.status(200).send(books);
}
};const express = require("express");
const bc = require("./controllers/books_controller.js");
const app = express();
app.use(express.json());
app.get("/api/books", bc.read);
app.post("/api/books", bc.create);
app.put("/api/books/:id", bc.update);
app.delete("/api/books/:id", bc.delete);
const port = 4000;
app.listen(port, () => {
console.log(`Server listening on port ${port}`);
});In this step, we will use Postman Unit Tests to test our endpoints.
In this step, we will use express.static to serve up our index.html file. express.static takes an argument that is the location of the folder with the files you want to serve when the server URL is hit in a browser. Our front-end was made using create-react-app and instead of running it's own server, we have made a production ready build so that our server can serve up our frontend. We'll want to serve the entire build folder.
const express = require("express");
const bc = require("./controllers/books_controller.js");
const app = express();
app.use(express.json());
app.use(express.static(__dirname + "/../build"));
app.get("/api/books", bc.read);
app.post("/api/books", bc.create);
app.put("/api/books/:id", bc.update);
app.delete("/api/books/:id", bc.delete);
const port = 4000;
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 |