How can I output the rows(docs) as html table format without using express/view engine. I tried writing res.write(${documents}) which is not working. Please check the following code.
const http = require('http');
const MongoClient = require('mongodb').MongoClient;
const hostname = '127.0.0.1';
const port = 3000;
const url = 'mongodb://localhost:27017';
const client = new MongoClient(url, { useNewUrlParser: true });
var documents;
client.connect(err => {
const collection = client.db("mydb").collection("messages");
collection.find({}).toArray(function(err, docs) {
console.log("Found the following records");
console.log(docs); // How can write this docs as html table
documents = docs; // Is there any workaround for this?
});
client.close();
});
const server = http.createServer((req,res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.write(`Server running at http://${hostname}:${port}/. Records:${documents}`); //this document not appearing
res.end();
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});