4. RESTful APIs with Node.js
Activity 5: Creating an API Endpoint for a Keypad Door Lock
Solution
- Create a new project folder and change the directories going to it to the following:
mkdir passcode cd passcode
- Initialize an
npm
project and installexpress
,express-validator
, andjwt-simple
. Then, make a directory forroutes
:npm init -y npm install --save express express-validator jwt-simple mkdir routes
- Create a
config.js
file, just as you did in Exercise 21, Setting Up an Endpoint that Requires Authentication. This should contain a randomly generated secret value:let config = {}; // random value below generated with command: openssl rand -base64 32 config.secret = "cSmdV7Nh4e3gIFTO0ljJlH1f/F0ROKZR/hZfRYTSO0A="; module.exports = config;
- Make the
routes/check-in.js
file in order to create a check-in route. This can be copied in whole from Exercise 21, Setting Up an Endpoint that Requires Authentication:const express = require('express'); const...