Solution 1 :

Based on this answer

With folder called assets contains app.css and jquery.js, also server.js and index.html with following content hopefully you could get your desired result.


const http = require("http");
const url = require("url");
const path = require("path");
const fs = require("fs");

const port = process.argv[2] || 8888;
const contentTypes = {
    '.html': "text/html",
    '.css':  "text/css",
    '.js':   "text/javascript"

http.createServer((req, res) => {
    let uri = url.parse(req.url).pathname;

    if (uri.indexOf('/api/getall') > -1 ) {
        // simulate database delay
        setTimeout(() => {
            res.writeHead(200, {"Content-Type": "application/json"});
            res.write(JSON.stringify({id: 10}));
        }, 1000);
    else {
        let filename = path.join(process.cwd(), uri);

        fs.exists(filename, exists => {
            if(!exists) {
                res.writeHead(404, {"Content-Type": "text/plain"});
                res.write("404 Not Foundn");

            if (fs.statSync(filename).isDirectory()) filename += '/index.html';

            fs.readFile(filename, "binary", (err, file) => {
              if (err) {        
                res.writeHead(500, {"Content-Type": "text/plain"});
                res.write(err + "n");

            let headers = {},
                contentType = contentTypes[path.extname(filename)];

              if (contentType) headers["Content-Type"] = contentType;

              res.writeHead(200, headers);           
              res.write(file, "binary");
}).listen(parseInt(port, 10));


<!DOCTYPE html>
<html lang="en">
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link href='/assets/app.css' rel='stylesheet' />
    <h1>Hello world!</h1>

    <script src='/assets/jquery.js'></script>
        $.ajax('/api/getall').done(res => console.log(res));

Run the app using node server.js and head to http://localhost:8888/, you should see Hello world on the screen and { id: 10 } on browser console. from there you can decide how to render your data, e.g using react or mustache.js

Problem :

I have a node.js application that is getting a collection from MongoDB and returning it correctly. What is the simplist way I can display this information into HTML or React? I am going to be hosting this later, I tried using a MERN approach, but I feel like express was overkill as I am just trying to read the data. I also could not host the app when I used express. I do not need anything else.

const {MongoClient} = require('mongodb');

async function main(){
     * Connection URI. Update <username>, <password>, and <your-cluster-url> to reflect your cluster.
     * See for more details
    const uri = "mongodb+srv://mongodbTest:[email protected]/test?retryWrites=true&w=majority"

    const client = new MongoClient(uri);

    try {
        // Connect to the MongoDB cluster
        await client.connect();

        // Make the appropriate DB calls
        await  listDatabases(client);
        await findOneListingByName(client, "o2");

    } catch (e) {
    } finally {
        await client.close();


async function listDatabases(client){
    databasesList = await client.db().admin().listDatabases();

    databasesList.databases.forEach(db => console.log(` - ${}`));

async function findOneListingByName(client, nameOfListing) {
    result = await client.db("testing").collection("testcollection").findOne({ name: nameOfListing }

    if (result) {
        console.log(`Found a listing in the collection with the name '${nameOfListing}':`);
    } else {
        console.log(`No listings found with the name '${nameOfListing}'`);


Comment posted by Mehdi Dehghani

It’s not overkill, you add it to your application and with few lines of code, you can serve up your content. also it can help you in future if you decide to add more pages to your app.

Comment posted by Using node.js as a simple web server

Does this answer your question?

Comment posted by makingithappeninc

@MehdiDehghani not quite I have a function in node.js that i returns a value I need displayed in my html/react

Comment posted by Mehdi Dehghani

Share your code please.

Comment posted by makingithappeninc

@MehdiDehghani this is my node.js file, the value I get for logs correctly, but I need it displayed