Initialize and run the app: npm install
&& npm start
.
A web page has been created for us. Our job is to build the server.
- This is the starting point of the server, put all code here
- After "app" is defined, add the
express.static
middleware to express referencing the "public" folder - Navigate to http://localhost:4000 to see the web page
- After "express.static", add body parser middleware to express
- Remember:
bodyParser.json()
- Remember:
- Between lines 2 and 3, require the data for from the following .js files:
- /data/contacts
- /data/vehicles
- /data/comments
- /data/products
- Add
app.get()
routes for the following:- "/contacts"
- "/vehicles"
- "/comments"
- "/products"
res.json()
the appropriate array from the "require" statements above
- Add
app.get()
routes for the following:- "/contacts/:id"
- "/vehicles/:id"
- "/comments/:id"
- "/products/:id"
- Make sure to include the path variable for id (above)
- Use
req.params.id
to .find() the item from the appropriate array (by its _id)- Example:
i._id == req.params.id
- Example:
res.json()
the item found
- Add
app.post()
routes for the following:- "/contacts"
- "/vehicles"
- "/comments"
- "/products"
- Add the information from the body to the appropriate array
- Figure out how to increment each
_id
by one and add this _id to "req.body" - Add
postId: 1
to "req.body" - Finally..
<ARRAY>.push(req.body)
- Figure out how to increment each
Once completed, the web page should be able to list out the information, create new information and view one item of the information. We don't need to worry about update or delete.
- Create a folder called "routes" on the top level (next to data)
- Create the following route files under this folder:
- contacts.js
- vehicles.js
- comments.js
- products.js
- The first two lines of each file should consist of:
const express = require('express')
const router = express.Router()
- Use the router to move the appropriate routes into the appropriate files
- Ex.
app.get('/contacts')
in "index.js" becomesrouter.get('/contacts')
in "routes/contacts.js" - Make sure you move the appropriate "data" from the index.js file into the new file
- Make sure you export the router from each file
- Ex.
- Import/require and use the routers in index.js
- Ex. for each..
app.use(<IMPORTED_ROUTER>)
- Ex. for each..
- Create a folder called "controllers" on the top level (next to data, routes)
- Create the following controller files under this folder:
- contacts.js
- vehicles.js
- comments.js
- products.js
- Move the appropriate "data" from the router files to the controller files
- Ex. move
const comments = require("../data/comments")
into the controller file so it has access to the array
- Ex. move
- Take the code from each router file and move it into the corresponding controller file
- Make variables (
const <something>
) for each function - We will make three functions in each file: "list", "show" and "create"
- You should be able to determine which is which, for example the GET routes will be "list" and "show"
- Export the functions at the end like:
module.exports = { list, show, create }
- Make variables (
- Import and use the controller functions in the appropriate router
- Ex.
app.get('/comments', commentController.list)
- Ex.
- When complete your routes files should look like this:
const express = require('express')
const router = express.Router()
const vehiclesController = require('../controllers/vehicles')
router.get('/vehicles', vehiclesController.list)
router.get('/vehicles/:id', vehiclesController.show)
router.post('/vehicles', vehiclesController.create)
module.exports = router