Skip to content

Instantly share code, notes, and snippets.

@simplesNotEZ
Last active August 29, 2018 17:02
Show Gist options
  • Select an option

  • Save simplesNotEZ/2ef66c698751f7c44c96efad95c069ba to your computer and use it in GitHub Desktop.

Select an option

Save simplesNotEZ/2ef66c698751f7c44c96efad95c069ba to your computer and use it in GitHub Desktop.
Express Cheatsheet

Set-up express project in folder

separate from any Galvanize forked & cloned project //so that your .git stuff is not mixed up with their .git stuff

  1. $ npm init -y

    //creates package.json which allows you to set-up your project dependencies--js libraries for your project

    //create a start script in your package.json: "start": "node app.js"

    //it may also be required to make sure that your "main" a couple lines above is like so: "main": "app.js",

  2. $ npm install express cors body-parser convert-csv-to-json

    //don't use express generator for now

    //gives access to Cross Origin Resource Sharing

  3. $ touch app.js

    in the app.js file:

    const express = require("express")       //knows to go look in node_modules
    
    const app = express( )
    
    const cors = require('cors')
    
    const csvToJson = require('convert-csv-to-json')
    
    //example code:  csvToJson.fieldDelimiter(',').getJsonFromCsv(file_name.csv);
    
    const port = process.env.PORT || 9000      //The "pocess.env.PORT" part is for heroku port variable that they assign
    
    app.listen(port, ( ) => { console.log('I am listening on ${port}') })   
         
         //single quotes above in console.log statement should actually be backticks in the code
  4. $ npm start or $ nodemon

    //to get the server running

  5. -in app.js file:

    app.use(cors( ));

    //now I'll be able to actually use cors

  6. -in app.js file:

    const bodyParser = require('body-parser')
    
    app.use(bodyParser.json( ))
    
    app.get( '/',  (request, response, next) => {
     })

Setting up separate github repository

  1. new repository
  2. $ touch .gitignore
  3. paste in .gitignore instructions from "the last .gitignore you'll ever need"
  4. $ git init //creates .git directory in your project folder
  5. $ git remote add origin (url to repo)
  6. $ git add -A //to stage all the files that you have created but not yet pushed to github repo (only do this after the .gitnore file has been created and populated, esp. with the node-modules file to be ignored)
  7. $ git commit -m "first commit"
  8. $ git push -u origin master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment