Skip to content

Instantly share code, notes, and snippets.

View Freizello's full-sized avatar

Hendriktio Freizello Freizello

  • Indonesia
View GitHub Profile
@Freizello
Freizello / index.js
Created December 18, 2018 06:53
[Route Example for ExpressJS - part 2] We've set up all our new routes, but we still have a route to the original page. Let's instead redirect this to the new index page that we've created at the path '/catalog'. #expressjs #nodejs
// GET home page.
router.get('/', function(req, res) {
res.redirect('/catalog');
});
@Freizello
Freizello / catalog.js
Created December 18, 2018 06:52
[Route Example for ExpressJS - part 1] Next we create routes for all the URLs needed by the LocalLibrary website, which will call the controller functions we defined in the previous section. The skeleton already has a ./routes folder containing routes for the index and users. Create another route file — catalog.js — inside this folder, as shown.…
var express = require('express');
var router = express.Router();
// Require controller modules.
var book_controller = require('../controllers/bookController');
var author_controller = require('../controllers/authorController');
var genre_controller = require('../controllers/genreController');
var book_instance_controller = require('../controllers/bookinstanceController');
/// BOOK ROUTES ///
@Freizello
Freizello / authorController.js
Last active December 18, 2018 06:39
[Controller Example for ExpressJS] Before we define our routes, we'll first create all the dummy/skeleton callback functions that they will invoke. The callbacks will be stored in separate "controller" modules for Books, BookInstances, Genres, and Authors (you can use any file/module structure, but this seems an appropriate granularity for this …
// get author model
var Author = require('../models/author');
// Display list of all Authors.
exports.author_list = function(req, res) {
res.send('NOT IMPLEMENTED: Author list');
};
// Display detail page for a specific Author.
exports.author_detail = function(req, res) {
@Freizello
Freizello / sequelize.js
Last active December 18, 2018 06:39
[ExpressJS with Sequelize] equelize.js will be the place to bootstrap our ORM and define relationships. We will define our models in their respective files and app.js will be our Express app. #nodejs #expressjs #mysql
const Sequelize = require('sequelize')
// models is optional based on app
const UserModel = require('./models/user')
const BlogModel = require('./models/blog')
const TagModel = require('./models/tag')
const dbname = 'DB_NAME';
const dbuser = 'DB_USER';
const dbpass = 'DB_PASS';