-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
47 lines (29 loc) · 2.64 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
const express = require('express');
const path = require('path');
const cookieParser = require('cookie-parser');
const logger = require('morgan');
const indexRouter = require('./routes/index');
const recipesRouter = require('./routes/recipes'); // sets up the route for recipes endpoint
const cors = require('cors'); // imports the cors module
const app = express();
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(cors()); // sets up cors middleware to run inbetween the user's request and the server, allowing for cross origin resource sharing
app.use('/', indexRouter);
app.use('/recipes', recipesRouter); // middleware that redirects users to use the recipesRouter route when they go to /recipes
module.exports = app;
/* Steps I took to start the API:
1. From your workspace, use the command: 'npx express-generator --no-view <name-of-your-app>'
Replace <name-of-your-app> with your planned API name (making sure to use dashes instead of spaces if needed). This will create a brand new directory with a boilerplate routes file structure.
2. Following the instructions listed from express-generator that were printed into your terminal after install, navigate into the newly-created directory and run the command: 'npm install' to install all the dependencies.
2a. Optional: For development purposes, I installed the 'nodemon' package with the command: 'npm install nodemon -D'
This installs nodemon as a developer dependency, meaning if a user were to fork and clone down my API and run 'npm install', it won't be added into their 'node_modules' file as it was only needed for development. The purpose of nodemon is to automatically refresh your server after changes are made, instead of having to close and restart the server every time.
If you do this step, go into your package.json file and modify the 'start' command under 'scripts' to say 'nodemon' instead of 'node' - now when you do 'npm start', nodemon works and automatically refreshes your server when you make changes.
3. I created a '.gitignore' file and added 'node_modules' so that it wouldn't get pushed with the rest of the files to GitHub
4. I created a git repository and went through the usual steps to initialize the directory, add, commit, and push
5. I created a top-level folder called 'data' in the project directory which I used to store the .JSON version of the given data in a file called 'recipes.json'
6. I renamed the default 'users.js' file under the 'routes' folder to my planned route, then redid the code inside with the methods that I wanted.
*/