/my-first-express-app-ta

Class notes from My First Express App lecture!

Primary LanguageJavaScript

my-first-express-app-ta

Steps for Creating Express App

  1. Create a new GH repo with readme
  2. open in vscode
  3. run npm init -y
  • this creates a package.json file
  1. ACP
  2. add .gitignore and add node_modules inside file
  3. create routes in index.js
  4. nodemon index.js to run server

Deploying App to Heroku

  1. heroku create app-name
  2. 2 URLs returned - ...herokuapp.com & ...app-name.git
  3. git remote -v --> shows heroku remote url
  4. add script to package.json for starting app
  • "start": "node index.js"
  • Nodemon for constantly listening for changes: "start:watch": "nodemon index.js"
  1. push to heroku (from main): git push heroku main
  2. push to heroku (from dev): git push heroku dev:main
  3. once deploy has successfully completed --> you can go to ...herokuapp.com and view the deployed site!

Data

  1. add a data.js file
  2. create data and save to a variable --> then module.exports = dataVariable
  3. go to index.js and import data const data = require('./data.js');
  4. can res.send(data) in an api endpoint
  5. NOTE: undefined route error: Cannot GET /blah

Testing Endpoints

  1. create a __test__ file with a name.test.js file
  2. rename index.js to server.js
  3. add app.js
  4. server.js is responsible for listening
  5. app.js responsible for the logic
  6. npm i supertest -- for making requests in testing to your apps endpoints
  7. npm i jest -- for testing
  8. add test script "test": "jest"
  9. create it block with test
  10. npm run test -- which runs jest