@app.js
const indexRouter = require('./routes/index');
const usersRouter = require('./routes/users');
const canvasRouter = require('./routes/canvas');
app.use('/', indexRouter);
app.use('/users', usersRouter);
app.use('/canvas', canvasRouter);
app.set('view engine', 'ejs');
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.
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
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.
express -v hbs signup-form
npm i
npm install hbs@4.1.0
# EJS <<- my favorite
> npm install ejs
# PUG
> npm install pug
# Mustache
> npm install mustache
# Handlebars
> npm install handlebars
# Marko
> npm install marko