Express starting - tdkehoe/blog GitHub Wiki

Documentation at: http://expressjs.com/starter/installing.html

#Install Express and Git

mkdir learn_express
cd learn_express
touch app.js
npm init --yes
git init
echo "node_modules" > .gitignore
git add -A
git commit -m "initial commit"

Install Express:

npm install express --save

#Set Up app.js

var express = require('express');
var app = express();

// Homepage route, when the browser sends a GET request to the server
app.get("/", function (req, res) {
  // Send response to the browser: "Hello world"
  res.send("Hello world");
})

// Another route
app.get("/meaning-of-life", function (req, res){
  res.send("42");
})

// Start server
app.listen(3000, function() {
  console.log("Serving on http://localhost:3000")
})

#Start Server

node app.js

or

nodemon

#Express With Angular npm install each of the following dependencies, starting with Express:

  "dependencies": {
  "body-parser": "^1.13.3",
    "cookie-parser": "^1.3.5",
    "cors": "^2.7.1",
    "debug": "^2.2.0",
    "express": "^4.13.3",
    "morgan": "^1.6.1",
    "path": "^0.11.14",
    "serve-favicon": "^2.3.0"
},

For Heroku, add a start script to package.json:

"scripts": {
   "start": "node app.js",
   "test": "echo \"Error: no test specified\" && exit 1"
 },

Then run

npm install

Touch in the app root app.js and index.html.

This block is necessary in app.js:

app.use(function(req, res) {
  res.sendFile(__dirname + '/index.html');
});

This block makes a single page app to send up a static file instead of refreshing the page when routes change.

Make directory public. (Critical.)

In public make directory partials, stylesheets, sass, js. Make Angular files controllers.js, module.js, routes.js.

On the command line run in separate tabs

node-sass --watch public/sass/ -o public/stylesheets/ 

nodemon app.js

To use SASS: write SASS in sass/style.scss and have an empty file stylesheets/style.css. SASS writes to the regular CSS file (which you don't touch). In index.html link to the regular CSS stylesheet, not to the SASS stylesheet.

To use CSS: write CSS in stylesheets/style.css.

In index.html, script links to public start with a slash, not with public/.

<script src="/module.js"></script>

this equals

<script src="public/module.js"></script>

If you use a js directory inside public, then the script links for index.html have to start with /js/filename.js.

The order of script lines in index.html must be modules.js, controllers.js, routes.js.

<script src="/js/module.js"></script>
  <script src="/js/controllers.js"></script>
  <script src="/js/routes.js"></script>

This should deploy to heroku and doesn't require SuperStatic.

Check If Express Is Running

npm run start

Open your browser to localhost:3000

This is set in app.js:

var port = normalizePort(process.env.PORT || '3000');

#Restart Server After Saving Changes

This isn't nodemon.

#Hiding Keys in .env

Make .env directory.

KEY_NAME=rgwegthth

process.env.KEY_NAME

At the top of app.js:

require('dotenv').load();

then in the CLI:

npm install dotenv --save

Then set up Heroku:

heroku config
⚠️ **GitHub.com Fallback** ⚠️