title: Best practices for MySQL + Node/Express + Angular Stack
body: <p>I am currently using MySQL for the db instead of the popular mongodb, since that is the case there isn't much documentation out there as far as architecture and getting set up. This is my current structure</p>
  
  <pre><code>client
  -- angular files
  routes
  -- index.js
  views
  -- 404 page
  app.js
  </code></pre>
  
  <p>I don't understand how I can implement controllers or models into this structure. I'm currently grabbing data from the db or sending it with the routes..I'm not sure what the added layer of controllers would do. Maybe this is a dumb question but I would just like to have a clear baseline so that my project will scale well. I feel like there should be way more to this than what I currently have. </p>
  
  <p>index.js</p>
  
  <pre><code>const express = require('express');
  const mysql = require('mysql');
  const router = express.Router();
  
  const db = mysql.createConnection({
      host     : 'localhost',
      user     : 'root',
      password : 'password',
      database : 'db'
  });
  
  // Connect
  db.connect((err) =&gt; {
      if(err){
          throw err;
      }
      console.log('MySql Connected...');
  });
  
  // Select Data
  router.get('/getData', (req, res) =&gt; {
      let sql = 'SELECT * FROM data';
      let query = db.query(sql, (err, results) =&gt; {
          if(err) throw err;
          console.log(results);
          res.send(results)
      });
  });
  
  module.exports = router;
  </code></pre>
  
  <p>app.js</p>
  
  <pre><code>const express = require('express');
  const mysql = require('mysql');
  const bodyParser = require('body-parser');
  const path = require('path');
  const cors = require('cors');
  const compression = require('compression');
  const helmet = require('helmet')
  const expressSanitizer = require('express-sanitizer');
  
  const index = require('./routes/index');
  
  const app = express();
  const port = 3000;
  
  var corsOptions = {
      origin: 'http://localhost:8100',
      optionsSuccessStatus: 200 // some legacy browsers (IE11, various SmartTVs) choke on 204 
  }
  
  // var logger = (req, res, next) =&gt; {
  //     console.log('logging...')
  //     next();
  // }
  
  //added security
  app.use(helmet())
  
  // //set logger
  // app.use(logger)
  
  //cors options
  app.use(cors(corsOptions))
  
  //body parser middleware
  app.use(bodyParser.json())
  app.use(bodyParser.urlencoded({extended: false}))
  
  // Mount express-sanitizer here
  app.use(expressSanitizer()); // this line follows bodyParser() instantiations
  
  //set static path
  app.use(express.static(path.join(__dirname, 'client')));
  
  // set our default template engine to "ejs"
  // which prevents the need for using file extensions
  app.set('view engine', 'ejs');
  
  //gzip compression
  app.use(compression())
  
  //set views for error and 404 pages
  app.set('views', path.join(__dirname, 'views'));
  
  app.use('/', index);
  app.use('/fp/trips', trips);
  
  app.listen(port, () =&gt; {
      console.log('server started on port 3000')
  })
  </code></pre>