Skip to content

Commit

Permalink
BE #1 application bootstrap
Browse files Browse the repository at this point in the history
  • Loading branch information
tito433 committed May 6, 2020
1 parent e06f1e1 commit 2fc474c
Show file tree
Hide file tree
Showing 9 changed files with 184 additions and 47 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ node_modules
dist
.idea
application.log
static/files/*
cache_temp/*
static_temp/*
.vscode
Expand Down
41 changes: 33 additions & 8 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,37 @@
const express = require('express');
const logger = require('morgan');
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var logger = require('morgan');

const app = express();
var indexRouter = require('./routes/index');

app.get('/', (req, res) => {
res.send('Hello CMS!')
var app = express();

// 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: false }));
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', indexRouter);

// 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');
});

app.listen(3000, () => {
console.log('Server started and listening on PORT: 3000');
});
module.exports = app;
90 changes: 90 additions & 0 deletions bin/www
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#!/usr/bin/env node

/**
* Module dependencies.
*/

var app = require('../app');
var debug = require('debug')('cms-backend:server');
var http = require('http');

/**
* Get port from environment and store in Express.
*/

var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);

/**
* Create HTTP server.
*/

var server = http.createServer(app);

/**
* Listen on provided port, on all network interfaces.
*/

server.listen(port);
server.on('error', onError);
server.on('listening', onListening);

/**
* Normalize a port into a number, string, or false.
*/

function normalizePort(val) {
var port = parseInt(val, 10);

if (isNaN(port)) {
// named pipe
return val;
}

if (port >= 0) {
// port number
return port;
}

return false;
}

/**
* Event listener for HTTP server "error" event.
*/

function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}

var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;

// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}

/**
* Event listener for HTTP server "listening" event.
*/

function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}
47 changes: 9 additions & 38 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,44 +1,15 @@
{
"name": "cms-backend-api",
"version": "1.0.0",
"description": "Simple CMS backend",
"main": "app.js",
"name": "cms-backend",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node app.js",
"test": "mocha"
"start": "node ./bin/www"
},
"author": "Satyajit Dey<satyajit@cefalo.com>",
"contributors": [
{
"name": "Ahmed Maruf",
"email": "maruf@cefalo.com"
},
{
"name": "Atiqul Alama",
"email": "atiqul@cefalo.com"
},
{
"name": "Rezaul Karim",
"email": "tito@cefalo.com"
},
{
"name": "Satyajit Dey",
"email": "satyajit@cefalo.com"
}
],
"license": "ISC",
"dependencies": {
"express": "4.17.1",
"morgan": "1.10.0",
"passport": "0.4.1"
},
"devDependencies": {
"eslint": "6.8.0",
"mocha": "7.1.2",
"nodemon": "2.0.3"
},
"repository": {
"type": "git",
"url": "https://github.com/Cefalo/cms-backend-api.git"
"debug": "~2.6.9",
"express": "~4.16.1",
"http-errors": "~1.6.3",
"jade": "~1.11.0",
"morgan": "~1.9.1"
}
}
8 changes: 8 additions & 0 deletions public/css/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
body {
padding: 50px;
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
}

a {
color: #00B7FF;
}
9 changes: 9 additions & 0 deletions routes/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var express = require('express');
var router = express.Router();

/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Cefalo CMS' });
});

module.exports = router;
5 changes: 5 additions & 0 deletions views/error.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
extends layout

block content
h1 #{error.status} #{message}
pre #{error.stack}
23 changes: 23 additions & 0 deletions views/index.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
doctype html
html
head
title= title
style(type='text/css').
html,body{height:100%}body{display: table;margin: 0 auto;font-size:3em;text-align: center}div{text-align:left;display: table-cell;vertical-align: middle}

body
div
pre
| /~\
| |oo ) Hello World!
| _\=/_
| ___ / _ \
| / ()\ //|/.\|\\
| _|_____|_ \\ \_/ ||
| | | === | | \|\ /| ||
| |_| O |_| # _ _/ #
| || O || | | |
| ||__*__|| | | |
| |~ \___/ ~| []|[]
| /=\ /=\ /=\ | | |
|_____[_]_[_]_[_]________/_]_[_\____________
7 changes: 7 additions & 0 deletions views/layout.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
doctype html
html
head
title= title
link(rel='stylesheet', href='/css/style.css')
body
block content

0 comments on commit 2fc474c

Please sign in to comment.