Skip to content

Instantly share code, notes, and snippets.

@openhubdev
Last active January 21, 2022 12:45
Show Gist options
  • Save openhubdev/2159f6c404334aec777771894d91f64e to your computer and use it in GitHub Desktop.
Save openhubdev/2159f6c404334aec777771894d91f64e to your computer and use it in GitHub Desktop.

EXPRESSJS 101

@app.js

Define the custom routes JS files

const indexRouter = require('./routes/index');
const usersRouter = require('./routes/users');
const canvasRouter = require('./routes/canvas');

Define the custom PATH

app.use('/', indexRouter);
app.use('/users', usersRouter);
app.use('/canvas', canvasRouter);

(default+) SET: view engine setup

app.set('view engine', 'ejs');

(default+) USE: set the middleware

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')));
  • logger: When you run your app, you might notice that all the routes that are requested are logged to the console. If you want to disable this, you can just comment out this middleware.
  • express.json: You might notice that there are two lines for parsing the body of incoming HTTP requests. The first line handles when JSON is sent via a POST request and it puts this data in request.body.
  • express.urlencoded: The second line parses query string data in the URL (e.g. /profile?id=5) and puts this in request.query.
  • cookieParser: This takes all the cookies the client sends and puts them in request.cookies. It also allows you to modify them before sending them back to the client, by changing response.cookies.
  • express.static: This middleware serves static assets from your public folder. If you wanted to rename or move the public folder, you can change the path here.

(default) USE: set the middleware

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));

uselful codes

exports

To make objects available outside of a module; in this "square.js" file:

exports.area = function(width) { return width * width; };
exports.perimeter = function(width) { return 4 * width; };
//or multiple in one:
module.exports = {
  area: function(width) {
    return width * width;
  },

  perimeter: function(width) {
    return 4 * width;
  }
};

Now, you can import this module using require(), and then call the exported method(s) as shown:

const square = require('./square'); // Here we require() the name of the file without the (optional) .js file extension
console.log('The area of a square with a width of 4 is ' + square.area(4));
  • res.download() Prompt a file to be downloaded.
  • res.end() End the response process.
  • res.json() Send a JSON response.
  • res.jsonp() Send a JSON response with JSONP support.
  • res.redirect() Redirect a request.
  • res.render() Render a view template.
  • res.send() Send a response of various types.
  • res.sendFile() Send a file as an octet stream.
  • res.sendStatus() Set the response status code and send its string representation as the response body.

Quick start examples

Signup form using HBS boilderplate

express -v hbs signup-form
npm i
npm install hbs@4.1.0

template engines

# EJS <<- my favorite
> npm install ejs 

# PUG
> npm install pug 

# Mustache
> npm install mustache 

# Handlebars
> npm install handlebars

# Marko
> npm install marko

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment