Node

Express Authentication
Express Authentication
ℹ️
Express is a quick and easy server thing for Node js

Setup

$ npm init
$ npm install express --save
 
Put this into index.js:
//use express const express = require('express') const app = express() const port = 3000 //define routes app.get('/', (req, res) => res.send('Hello World!')) app.get('/blah', (req, res) => res.send('Blah Blah')) app.get('/cat', (req, res) => res.send('Miao')) //tell app where to listen app.listen(port, () => console.log(`Example app listening on port ${port}!`))
Run with
$ node index.js
See the result at http://localhost:3000

Auto-Restart server on change with nodemon

$ npm install –g nodemon (if not installed already)
Then start app with  nodemon instead of node:
$ nodemon index.js

Routes

Routes are bits of code that run other code based on requests
app.get('/', // … is listening for get request at the root app.get('/blah', // … listening for get request at /blah app.get('/cat/:name', (req, res) => res.send('Pattern!')) //make it a pattern app.get('*', // … catches all other get requests not handled above
First route that matches is the only one that gets used!
//use express const express = require('express') const app = express() const port = 3000 //define routes app.get('/', (req, res) => res.send('Hello World!')) app.get('/blah', (req, res) => res.send('Blah Blah')) app.get('/cat', (req, res) => res.send('Miau')) app.get('/cat/:name', (req, res) => { res.send('Pattern! Name: ' + req.params['name']) console.log(req.params) }) //make it a pattern app.get('/cat/:name/:comment', (req, res) => res.send('Comment!')) //make it a pattern app.get('*', (req, res) => res.send('Everything else')) //catch all other get requests //tell app where to listen app.listen(port, () => console.log(`Example app listening on port ${port}!`))

EJS - Embedded JavaScript Templating

There are also a lot of other template engines, but let’s look at EJS:
$ npm install ejs --save
EJS files go into the views directory, then you can set it as the “view engine”
app.set("view engine", "ejs");
and render an EJS like this:
res.render('home.ejs');
<%= %> //this is treated as javascript in EJS (the '=' sign will make it display the result in html) <% %> //this is treated as js as well, but will not display!
To send variables along you can pass an object along:
res.render('home.ejs', {nameOfVar: valueOfVar});
Then we can use it in EJS with
<%= nameOfVar.toUpperCase() %>

Adding static assets like images and css

If we want to include css or js files in our EJS, then we need to tell the express server that it's okay to serve those up.
app.use(express.static("public"));
We can now include files from the “public” folder in our .ejs files!

Partial Files

We can include partial files in our EJS! For example to include footer.ejs from /views/partials we can do:
<%- include('partials/footer'); %>
 
 
 

On AWS

Spin up a new instance, allow TCP for desired ports from anywhere
$ sudo yum install -y gcc-c++ make
$ curl -sL https://rpm.nodesource.com/setup_13.x | sudo -E bash -
$ sudo yum install -y nodejs
Then
$ npm init
$ npm install express –save
Install mongoose
$ npm install mongoose --save

Leave a comment