Created
March 18, 2020 12:14
-
-
Save mssyogi/751be8d87006aa3ae538fc121696a23c to your computer and use it in GitHub Desktop.
securenodeserver
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'); | |
const fs = require('fs'); | |
const path = require('path'); | |
const cors = require('cors'); | |
const compression = require('compression'); | |
const helmet = require('helmet'); | |
const morgan = require('morgan'); | |
const Routes = require('./routes'); | |
const Database = require('./database'); | |
const logger = require('./config/logger'); | |
const middleware = require('./services/middleware'); | |
const config = require('./config'); | |
const authentication = require('./services/authentication'); | |
const service = require('./services'); | |
const http = require('http'); | |
const https = require('https'); | |
const router = new Routes(express.Router(), new Database(config.DB_CONNECTION_STRING), authentication).register(); | |
const app = express(); | |
const dir = './logs'; | |
/** | |
* Create Dir if not exist | |
*/ | |
try { | |
if (!fs.existsSync(dir)) { | |
fs.mkdirSync(dir); | |
} | |
} catch (e) { | |
logger.log('error', 'create directory error', { detail: e.toString() }); | |
} | |
/** | |
* List of all Middlewares used in project cors, compression, helmet | |
* */ | |
try { | |
app.use(middleware()); | |
app.use(express.static('assets')); | |
app.use(cors()); | |
app.use(compression()); | |
app.use(helmet()); | |
app.use(express.json()); | |
app.use(express.urlencoded({ extended: true })); | |
app.use('/api/', router); | |
app.set('view engine', 'ejs'); | |
app.all('/*', (req, res) => res.status(404).json()); | |
} catch (e) { | |
logger.log('error', 'middleware error', { detail: e.toString() }); | |
} | |
/** | |
* Generating access.log to generate the logs using morgan logs are generated in | |
* access.logs file inside logs folder | |
* */ | |
try { | |
app.use( | |
morgan('combined', { | |
stream: fs.createWriteStream(path.resolve(process.cwd(), 'logs', 'access.log'), { | |
flags: 'a', | |
}), | |
}), | |
); | |
} catch (e) { | |
logger.log('error', 'morgan error', { detail: e.toString() }); | |
} | |
//#changes | |
var options_https = { | |
key: fs.readFileSync('/home/cmaps/cert/server.key', 'utf8'), | |
cert: fs.readFileSync('/home/cmaps/cert/server.crt', 'utf8'), | |
//ca: fs.readFileSync('image/server_files/bundle_files/server.ca-bundle'), | |
requestCert: true, | |
rejectUnauthorized: false | |
}; | |
app.get('test', function (req, res) { | |
res.writeHead(200); | |
res.end("hello world\n"); | |
}); | |
var secureServer = https.createServer(options_https, app).listen(8020, () => { | |
console.log(">> Lokesh listening at port " + 8020); | |
}); | |
app.get('sach', function(req, res) { | |
console.log('secure srever start'); | |
res.end("hello sach world\n"); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment