Skip to content

Instantly share code, notes, and snippets.

@jonasmodie
Created July 10, 2018 23:06
Show Gist options
  • Save jonasmodie/52d24db6ff98a07faf31dd123f3b542a to your computer and use it in GitHub Desktop.
Save jonasmodie/52d24db6ff98a07faf31dd123f3b542a to your computer and use it in GitHub Desktop.
computer science project
//loading modules
var createError = require('http-errors');
var http = require('http');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var session = require('express-session');
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var app = express();
//create express server
// Create server and listen on port 3030.
http.createServer(app).listen(3030, function() {
console.log('Server running...');
});
//database connection
socketPath: '/var/run/mysqld/mysqld.sock'
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '2MM45s789',
database : 'Students'
},'request');
connection.connect(function(err){
if(!err) {
console.log("Database is connected ... nn");
} else {
console.log("Error connecting database ... nn");
}
});
connection.end();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
//cross origin configuration
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.use('/', indexRouter);
app.use('/users', usersRouter);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
next(createError(404));
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
// create handler for user registration
/*
exports.register = function(req,res){
// console.log("req",req.body);
var today = new Date();
var users={
"first_name":req.body.first_name,
"last_name":req.body.last_name,
"email":req.body.email,
"password":req.body.password,
"created":today,
"modified":today
}
connection.query('INSERT INTO Student SET ?',users, function (error, results, fields) {
if (error) {
console.log("error ocurred",error);
res.send({
"code":400,
"failed":"error ocurred"
})
}else{
console.log('The solution is: ', results);
res.send({
"code":200,
"success":"user registered sucessfully"
});
}
});
}
// validate credentials
exports.login = function(req,res){
var email= req.body.email;
var password = req.body.password;
connection.query('SELECT * FROM users WHERE email = ?',[email], function (error, results, fields) {
if (error) {
// console.log("error ocurred",error);
res.send({
"code":400,
"failed":"error ocurred"
})
}else{
// console.log('The solution is: ', results);
if(results.length >0){
if([0].password == password){
res.send({
"code":200,
"success":"login sucessfull"
});
}
else{
res.send({
"code":204,
"success":"Email and password does not match"
});
}
}
else{
res.send({
"code":204,
"success":"Email does not exits"
});
}
}
});
}
*/
module.exports = app;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment