Generation

code to explanationTue, 24 Jan 2023

var scraper = require('./scraper'); var express = require('express'); var bodyParser = require('body-parser'); const path = require('path'); const crypto = require('crypto'); const algorithm = 'aes-256-cbc'; const ini = require('ini'); var cookieParser = require('cookie-parser'); var session = require('express-session'); var jwt = require('jsonwebtoken') const config = ini.parse(require('fs').readFileSync('./config.ini', 'utf-8')); var app = express(); var port = process.env.PORT || 10000; var urlencodedParser = bodyParser.urlencoded({ extended: false }); app.use(express.static(path.join(__dirname, 'static'))); app.use(session({secret: 'secret'})); require('./extra-routes')(app); app.get('/', function (req, res) { res.send('Hello World!'); }); app.post('/scrape', urlencodedParser, function (req, res) { if(req.body.url === undefined || req.body.url === "") { res.send("Please enter a valid url"); } else { scraper.run(req.body.url).then((data) => { res.send(data); }).catch((e) => { console.log(e); res.send(e); }).finally(() => { res.end() }); } }); app.get('/admin', function (req, res) { res.sendFile(path.join(__dirname, '/templates/admin.html')); }); app.post('/admin/login', urlencodedParser, function (req, res) { if (req.body.username === 'admin' && req.body.password === 'admin') { let jwtSecretKey = config.jwt_secret_key; let data = { time: Date(), user: "admin", } const token = jwt.sign(data, jwtSecretKey); var d = {'success': true, 'token': token}; const key = config.key; const iv = config.iv; const cipher = crypto.createCipheriv(algorithm, key, iv); var encrypted = cipher.update(JSON.stringify(d), 'utf8', 'hex'); encrypted += cipher.final('hex'); res.send({"response": encrypted}); } else { res.send('Access Denied!'); } }); app.get('/admin/check', function (req, res) {; try { const token = req.header("Authorization"); const verified = jwt.verify(token, config.jwt_secret_key); if(verified){ return res.send("Successfully Verified"); }else{ return res.status(401).send(error); } } catch (error) { return res.status(401).send(error); } }); app.listen(port, '0.0.0.0', function () { console.log(`App listening on port ${port}!`); });

// example code here

Questions about programming?Chat with your personal AI assistant