I am working on a project with Angular, Express and MySQL using node-mysql dependency. I have the code working but wanted to know if I can make it more modular so that I can have a cleaner app.js
file.
Here is my current app.js file
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var mysql = require('mysql');
var app = express();
// MySQL database connection file
var connection = require('./database/connection');
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
// Development Settings
if (app.get('env') === 'development') {
// This will change in production since we'll be using the dist folder
app.use(express.static(path.join(__dirname, '../client')));
// This covers serving up the index page
app.use(express.static(path.join(__dirname, '../client/.tmp')));
app.use(express.static(path.join(__dirname, '../client/app')));
// ROUTES BEGIN
// SIGNUP ROUTE
app.post('/signup', function(req, res){
var body = req.body;
var post = {
email: body.email,
firstname: body.firstname,
lastname: body.lastname,
password: body.password2
};
connection.query('INSERT INTO users SET ?', post, function(err,result) {
// Neat!
res.json({
'msg': 'success!'
});
});
console.log(query.sql);
});
}
module.exports = app;
I have a separate connection.js file which exposes connection
var express = require('express');
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '',
database : 'cole'
});
connection.connect();
module.exports = connection;
What I want is to have a separate /routes/signup.js
file which has the SQL query for signup route and use it in app.js
Is this feasible?