Skip to content

Instantly share code, notes, and snippets.

@antydemant
Last active June 21, 2020 14:02
Show Gist options
  • Save antydemant/a495ee4318937c67756b44390e01afe7 to your computer and use it in GitHub Desktop.
Save antydemant/a495ee4318937c67756b44390e01afe7 to your computer and use it in GitHub Desktop.
Steps
  1. Create repository - ✅
  2. Apply .gitignore boilerplate for Node.js apps - ✅
  3. Initialize new project by npm init (package.json) -
  4. Install express framework https://www.npmjs.com/package/express (npm install express, then check package.json)
const express = require('express');

const server = express();
const port = 3000;

server.get('/', (req, res) => {
  res.send('You rock!');
});

server.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`);
});
  1. Install view engine EJS https://www.npmjs.com/package/ejs (npm install ejs, then check package.json) -
server.set('view engine', 'ejs');
server.set('views', './views');
  1. Create views folder in the root of project
  2. Create simple ejs view template such as index.ejs
  3. Trigger template render
  res.render('index');
  1. Create public folder and after that configure express configuration to look into this folder.
server.use(express.static('public'));
  1. Create express module with the following routes:
  • GET /auth/login (just page)
  • POST /auth/login (to process user's login data from the form inputs)
  • GET /auth/registration (just page)
  • POST /auth/registration (to process user's registration data from the form inputs)
const express = require('express');
const router = express.Router();

router.get('/login', function (req, res) {
  // some code
});

router.get('/registration', function (req, res) {
  // some code
});

router.post('/login', function (req, res) {
  // some code
});

router.post('/registration', function (req, res) {
  // some code
});

module.exports = router;
server.use('/', auth);
  1. Install helper package body-parser to read the data from POST requests https://www.npmjs.com/package/body-parser (npm install body-parser, then check package.json)
  2. Configure body-parser middleware
server.use(
  bodyParser.urlencoded({
    extended: false,
  })
);
const { username, password1, password2 } = req.body; // 
  1. Install MySQL package to work with DB queries https://www.npmjs.com/package/mysql (npm install mysql, then check package.json)
  2. Create module with MySQL configuration db/mysql.js
const mysql = require('mysql');
const connection = mysql.createConnection({
    host: '0.0.0.0',
    user: 'root',
    password: process.env.MYSQL_PASSWORD,
    database: 'demo',
});

connection.connect((error) => {
    if(error) {
        console.log(error);
    } else {
        console.log('Connected!');
    }
});

module.exports = connection;
const db = require('../db/mysql');

db.query('SELECT id, username, password FROM users WHERE username = ?', username, (err, results) => {
    if(err) console.error(err);
    const { password, id } = results[0];
    // some code
});
  1. Install express-session package to work with sessions https://www.npmjs.com/package/express-session (npm install express-session, then check package.json)
server.use(
  session({
    secret: 'my super duper secret',
    resave: false,
    saveUninitialized: false,
    cookie: {
      maxAge: 60000,
    },
  })
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment