//node js create api server without any dependency github autopilot // must use https and http module // https module is used for https server // http module is used for http server // https module is used for https server // npm install sqlite3 // sudo node index-local.js // sudo nodemon index-local.js // sudo nodemon index-local.js npm install nodemon -g const http = require('http'); const sqlite3 = require('sqlite3').verbose(); const https = require('https'); const fs = require('fs'); const url = require('url'); const StringDecoder = require('string_decoder').StringDecoder; const crypto = require('crypto'); const console = require('console'); //const hostname = '0.0.0.0'; const hostname = '127.0.0.1'; //const port = 443; const portname = 8000; const domain ='example.com' //read the key and certificate /**const options = { key: fs.readFileSync('/private_key.key'), cert: fs.readFileSync('/ssl_certificate.cer') };**/ function get_db(){ //create db connection to sqlite3 database file db.sqlite3 in the same directory as this file index.js let db = new sqlite3.Database('./db.sqlite3', (err) => { if (err) { console.error(err.message); return; } else { console.log('Connected to the database.'); } }); return db; } // Redirect from http port 80 to https http.createServer(function (req, res) { //switch based on the url const parsedUrl = url.parse(req.url, true); const path = parsedUrl.pathname; const trimmedPath = path.replace(/^\/+|\/+$/g, ''); console.log(trimmedPath); switch(trimmedPath){ case '': //if the path is empty then redirect to the home page res.writeHead(200, {"content-type":"text/html"}); //get file content as the template fs.readFile('index.html', function(err, data) { if (err) { console.log(err); }else{ res.writeHead(200, {"content-type":"text/html"}); res.end(data); } }); break; case 'favicon.ico': //load favicon from file fs.readFile('favicon.ico', function(err, data) { if (err) { console.log(err); }else{ //send favicon as a file buffer data res.writeHead(200, {"content-type":"image/x-icon"}); res.end(data); } }); break; case 'api/auth': //if the path is api/auth then redirect to the api/auth page //write happy message res.writeHead(200, { 'Content-Type': 'application/json' }); res.end('{"message":"Welcome to the API"}'); break; case 'api/users': //show all users db = get_db(); db.all("SELECT * FROM users", function(err, rows) { if (err) { console.log(err.message); }else{ console.log('Users listed'); res.writeHead(200, { 'Content-Type': 'application/json' }); res.end(JSON.stringify(rows)); } }); db.close(); break; case 'api/users/add': //get form data var decoder = new StringDecoder('utf-8'); var buffer = ''; req.on('data', function(data) { buffer += decoder.write(data); }); req.on('end', function() { buffer += decoder.end(); //parse the data let data = null; //catch the error if Json is not valid try { data = JSON.parse(buffer); //connect to sqlite3 database, create table if not exist db = get_db(); db.run("INSERT INTO users (name, email, password, created_at, updated_at) VALUES (?, ?, ?, ?, ?)", [data.name, data.email, data.password, new Date(), new Date()], function(err) { if (err) { console.log(err.message); }else{ console.log('User created'); } }); db.close(); res.writeHead(200, {"content-type":"text/html"}); res.end('{"message":"User created"}'); } catch (e) { res.writeHead(400, {"content-type":"text/html"}); res.end('{"message":"Invalid JSON"}'); } }); break; case 'init_db': //if the path is init then redirect to the init page //connect to sqlite3 database, create table if not exist db = get_db(); db.run("DROP TABLE IF EXISTS users", function(err) { if (err) { console.log(err.message); }else{ console.log('Table dropped'); } }); db.run("CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, email TEXT, " + "password TEXT, created_at TEXT, updated_at TEXT)", function(err) { if (err) { console.log(err.message); }else{ console.log('Table created'); } }); db.close(); res.writeHead(200, {"content-type":"text/html"}); res.end('{"message":"Table created"}'); break; default: //if the path is not empty then redirect to the home page res.writeHead(404, {"content-type":"text/html"}); res.end('404 Not Found'); break; } }).listen(portname, hostname);