Skip to content
This repository was archived by the owner on Feb 4, 2024. It is now read-only.

Commit 0aab73e

Browse files
committed
Release v1.1.0
1 parent f9bfd55 commit 0aab73e

File tree

11 files changed

+3565
-258
lines changed

11 files changed

+3565
-258
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55

66
node_modules/
77

8-
config/sysConfig\.json
8+
config/config\.json

README.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
# Base WebFramework NodeJS
22
[![Build Status](https://travis-ci.org/RAK3RMAN/base-webframework-nodejs.svg?branch=master)](https://travis-ci.org/RAK3RMAN/base-webframework-nodejs)
33

4-
A general template for a nodejs web application running express
4+
A general template for a Node.js web application running Fastify and Handlebars
55

66
### Basic Structure
7-
This project is a base web framework to run a web application using express through NodeJS. Being a 'base' framework, this project only displays a webpage through a specified port with no authentication. The structure of this application is described below in the application map.
7+
This project is a base web framework to run a web application using Fastify through Node.js. Being a 'base' framework, this project only displays a webpage through a specified port with no authentication. The structure of this application is described below in the application map.
88

99
### Application Map
1010
```
1111
--app.js # Primary NodeJS file
1212
--routes # Routes for views
13-
--mainRoutes.js
14-
--views # Components of webpage, HTML
15-
--pages
16-
--home.ejs # Home Page
17-
--error.ejs # Error Page
13+
--error-routes.js
14+
--templates # Components of webpage, HTML
15+
--home.ejs # Home Page
16+
--error.ejs # Error Page
1817
--config # Folder where configurations are set
19-
--exitOpt.js # Exit options when running in testing environment
20-
--sysConfig.json # Appears upon system configuration within application
21-
--static # Place static files to be accessed by webpage here
22-
--package.json # NPM
18+
--testing.js # Exit options when running in testing environment
19+
--setup.json # Helper file for config.json
20+
--config.json # Appears upon system configuration within application
21+
--public # Place static files to be accessed by webpage here
22+
--package.json
2323
--package-lock.json
2424
--start.sh
2525
--LICENSE
@@ -29,7 +29,7 @@ This project is a base web framework to run a web application using express thro
2929
```
3030

3131
## Install and Setup
32-
- Clone the repository from github.com
32+
- Clone the repository from https://github.com
3333
```
3434
git clone https://github.com/RAK3RMAN/base-webframework-nodejs.git
3535
```
@@ -42,8 +42,8 @@ git clone https://github.com/RAK3RMAN/base-webframework-nodejs.git
4242
- `npm start`
4343
- If you want a different broadcast port, you can configure these values by proceeding with the:
4444
- Hardcode option:
45-
- Enter the `sysConfig.json` file
46-
- `sudo nano base-webframework-nodejs/config/sysConfig.json`
47-
- Edit the `console_port` parameter to your desired configuration
45+
- Enter the `config.json` file
46+
- `sudo nano base-webframework-nodejs/config/config.json`
47+
- Edit the `webserver_port` parameter to your desired configuration
4848
- If any errors occur, please read the logs and attempt to resolve. If resolution cannot be achieved, post in the issues under this project.
4949
- Access web application through `localhost:3000`

app.js

Lines changed: 86 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -1,122 +1,86 @@
1-
/*\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
2-
App/Filename : BASE/app.js
3-
Description : Initializes nodejs
4-
Author : RAk3rman
5-
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\*/
6-
7-
//===================================================//
8-
// --- Initialize Packages and Routers --- //
9-
//===================================================//
10-
11-
//Declare Packages
12-
let express = require('express');
13-
let session = require('express-session');
14-
let morgan = require('morgan');
15-
let createError = require('http-errors');
16-
let cookieParser = require('cookie-parser');
17-
let bodyParser = require('body-parser');
18-
let ip = require('ip');
19-
let uuidv4 = require('uuid/v4');
20-
21-
//Setup Local Database
22-
let dataStore = require('data-store');
23-
let storage = new dataStore({path: './config/sysConfig.json'});
24-
25-
//System Config Checks - - - - - - - - - - - - - - - - -
26-
//Session Secret Check
27-
let session_secret = storage.get('session_secret');
28-
if (session_secret === undefined) {
29-
let newSecret = uuidv4();
30-
storage.set('session_secret', newSecret);
31-
console.log('Config Manager: Session Secret Set - ' + newSecret);
32-
}
33-
//Console Port Check
34-
let console_port = storage.get('console_port');
35-
if (console_port === undefined) {
36-
storage.set('console_port', 3000);
37-
console.log('Config Manager: Port Set to DEFAULT: 3000');
38-
}
39-
//End of System Config Checks - - - - - - - - - - - - - -
40-
41-
//Declare App
42-
const app = express();
43-
app.set('view engine', 'ejs');
44-
45-
//Initialize Exit Options (for Testing Environments)
46-
let exitOpt = require('./config/exitOpt.js');
47-
setTimeout(exitOpt.testCheck, 3000);
48-
49-
//Routers
50-
let mainRoutes = require('./routes/mainRoutes.js');
51-
52-
//Express Processes/Packages Setup
53-
app.use(session({
54-
secret: storage.get('session_secret'),
55-
resave: true,
56-
saveUninitialized: true
57-
}));
58-
app.use(cookieParser());
59-
app.use(bodyParser.urlencoded({extended: true}));
60-
app.use(bodyParser.json());
61-
app.use(morgan('dev'));
62-
app.use(express.json());
63-
app.use(express.urlencoded({extended: false}));
64-
65-
//Import Static Files to Webpages
66-
app.use('/static', express.static(process.cwd() + '/static'));
67-
68-
//End of Initialize Packages and Routers - - - - - - - -
69-
70-
71-
//===================================================//
72-
// --- LEMAgent Config Routes/Logic --- //
73-
//===================================================//
74-
75-
//Create Routes
76-
app.get('/', mainRoutes.homeRoute);
77-
78-
//End of LEMAgent Config Routes/Logic - - - - - - - - -
79-
80-
81-
//===================================================//
82-
// --- Error Handlers --- //
83-
//===================================================//
84-
85-
//404 - Send to Error Handler
86-
app.use(function (req, res, next) {
87-
next(createError(404));
88-
});
89-
90-
// Error Handler Logic
91-
app.use(function (err, req, res, next) {
92-
//Determine Message
93-
res.locals.message = err.message;
94-
res.locals.error = req.app.get('env') === 'development' ? err : {};
95-
//Render Error Page
96-
res.status(err.status || 500);
97-
res.render('pages/error.ejs', {title: 'Error'});
98-
});
99-
100-
//End of Error Handler - - - - - - - - - - - - - - - - -
101-
102-
103-
//===================================================//
104-
// --- External Connections Setup --- //
105-
//===================================================//
106-
107-
//Port Listen
108-
let http = require('http');
109-
let server = http.createServer(app);
110-
server.listen(storage.get('console_port'), function () {
111-
console.log(' ');
112-
console.log('============================================');
113-
console.log(' Base-WebFramework-NodeJS | RAk3rman 2019 ');
114-
console.log('============================================');
115-
console.log('Web Page Accessable at: ' + ip.address() + ":" + storage.get('console_port'));
116-
console.log(' ');
117-
});
118-
119-
//End of External Connections Setup - - - - - - - - - -
120-
121-
//Export Express
122-
module.exports = app;
1+
/*\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
2+
Filename : base/app.js
3+
Desc : main application file
4+
Author(s): RAk3rman
5+
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\*/
6+
7+
// Packages and configuration - - - - - - - - - - - - - - - - - - - - - - - - -
8+
9+
// Declare packages
10+
const path = require('path');
11+
const dataStore = require('data-store');
12+
const config_storage = new dataStore({path: './config/config.json'});
13+
const chalk = require('chalk');
14+
const pkg = require('./package.json');
15+
const moment = require('moment');
16+
const wipe = chalk.white;
17+
18+
// Print header to console
19+
console.clear();
20+
console.log(chalk.blue.bold('\nBase Webframework v' + pkg.version + ((process.argv[2] !== undefined) ? ' | ' + process.argv[2].toUpperCase() : "" )));
21+
console.log(chalk.white('--> Contributors: ' + pkg.author));
22+
console.log(chalk.white('--> Description: ' + pkg.description));
23+
console.log(chalk.white('--> Github: ' + pkg.homepage + '\n'));
24+
25+
// Check configuration values
26+
let setup = require('./config/setup.js');
27+
setup.check_values(config_storage);
28+
29+
// End of Packages and configuration - - - - - - - - - - - - - - - - - - - - - -
30+
31+
// Fastify and main functions - - - - - - - - - - - - - - - - - - - - - - - - - -
32+
33+
// Declare fastify
34+
const fastify = require('fastify')({logger: false});
35+
36+
// Prepare rendering template
37+
fastify.register(require('point-of-view'), {
38+
engine: {
39+
handlebars: require('handlebars')
40+
},
41+
})
42+
fastify.register(require('fastify-static'), {
43+
root: path.join(__dirname, 'public'),
44+
prefix: '/public/',
45+
})
46+
// fastify.register(require('fastify-socket.io'), {})
47+
// fastify.register(require('fastify-formbody'))
48+
// fastify.register(require('fastify-rate-limit'), {
49+
// global: false,
50+
// max: 250,
51+
// timeWindow: '1 minute'
52+
// })
53+
54+
// Routers
55+
let error_routes = require('./routes/error-routes.js');
56+
57+
// Import routes
58+
error_routes(fastify);
59+
60+
// Home page
61+
fastify.get('/', (req, reply) => {
62+
reply.view('/templates/home.hbs', {
63+
title: "Home"
64+
})
65+
console.log(wipe(`${chalk.bold.magenta('Fastify')}: [` + moment().format('MM/DD/YY-HH:mm:ss') + `] GET /`));
66+
})
67+
68+
// End of Fastify and main functions - - - - - - - - - - - - - - - - - - - - - -
69+
70+
71+
// Setup external connections - - - - - - - - - - - - - - - - - - - - - - - - -
72+
73+
// Start webserver using config values
74+
console.log(wipe(`${chalk.bold.magenta('Fastify')}: [` + moment().format('MM/DD/YY-HH:mm:ss') + `] Attempting to start http webserver on port ` + config_storage.get('webserver_port')));
75+
fastify.listen(config_storage.get('webserver_port'), function (err) {
76+
if (err) {
77+
fastify.log.error(err)
78+
process.exit(1)
79+
}
80+
console.log(wipe(`${chalk.bold.magenta('Fastify')}: [` + moment().format('MM/DD/YY-HH:mm:ss') + `] Running http webserver on port ` + config_storage.get('webserver_port')));
81+
// Check if we are testing
82+
const testing = require('./config/testing.js');
83+
testing.testCheck();
84+
})
85+
86+
// End of Setup external connections - - - - - - - - - - - - - - - - - - - - - -

config/setup.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
2+
Filename : base/config/setup.js
3+
Desc : checks and sets up configuration values
4+
in config.json using data-store
5+
Author(s): RAk3rman
6+
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\*/
7+
8+
// Packages
9+
const chalk = require('chalk');
10+
const wipe = chalk.white;
11+
const moment = require('moment');
12+
13+
// Name : setup.check_values()
14+
// Desc : checks all config.json values and configures each value if invalid
15+
// Author(s) : RAk3rman
16+
exports.check_values = function (config_storage) {
17+
console.log(wipe(`${chalk.bold.cyan('Setup')}: [` + moment().format('MM/DD/YY-HH:mm:ss') + `] Checking configuration values`));
18+
let invalid_config = false;
19+
// Config value: webserver_port | the port where the webserver will listen for requests
20+
if (!config_storage.has('webserver_port') || config_storage.get('webserver_port') === '') {
21+
config_storage.set('webserver_port', 3000);
22+
console.log(wipe(`${chalk.bold.cyan('Setup')}: [` + moment().format('MM/DD/YY-HH:mm:ss') + `] "webserver_port" value in config.json set to default: "3000"`));
23+
}
24+
// Exit if the config values are not set properly
25+
if (invalid_config) {
26+
console.log(wipe(`${chalk.bold.cyan('Setup')}: [` + moment().format('MM/DD/YY-HH:mm:ss') + `] Please check "config.json" and configure the appropriate values`));
27+
process.exit(0);
28+
} else {
29+
console.log(wipe(`${chalk.bold.cyan('Setup')}: [` + moment().format('MM/DD/YY-HH:mm:ss') + `] Configuration values have been propagated`));
30+
}
31+
}
File renamed without changes.

0 commit comments

Comments
 (0)