0

I was trying to input Text data to a MongoDB database using just a HTML form. But when I ran it locally it doesn't work. I think this had something to do with creating a Node.js server. But I can't figure out how to run a HTML file (which is index.html here). I have only learned to run just the JavaScript code alone in the Node console. I don't know how I can run this index.html locally on NodeJS.

Also I want to do this without using ExpressJS! Everything I found online showed only on how to do this using ExpressJS. Is there a reason behind it? Can't we able to do this using just NodeJS and MongoDB? (LOCALLY on Windows)

var MongoClient = require("mongodb").MongoClient;
var url = "mongodb://localhost:27017";

function addData(){
    var record = document.getElementById("title").value;
    MongoClient.connect(url, function (err, db) {
        if (err) throw err;
        var dbo = db.db("notetest");
        var record2value = { title: record };
        dbo.collection("page1").insertOne(record2value, function (err, res) {
            if (err) throw err;
            console.log("1 document added!");
            db.close();
        })
    })
};

<!DOCTYPE html>
<html>
<head>
    <title>test</title>
    <script type="text/javascript" src="scripts/script.js"></script>
</head>
<body>
    <form onsubmit="addData()">
        <input type="text" name="title" id="title">
        <input type="submit" name="submit" id="submit">
    </form>
</body>
</html>

UPDATE: Also I tried calling this index.html using Node. By using the following code.

var http = require('http');
var fs = require('fs');

const PORT=8080; 

fs.readFile('./index.html', function (err, html) {

    if (err) throw err;    

    http.createServer(function(request, response) {  
        response.writeHeader(200, {"Content-Type": "text/html"});  
        response.write(html);  
        response.end();  
    }).listen(PORT);
});

But I don't know why I am not seeing the console.log from MongoDB, which should say "1 document added!".

Remix Protocol
  • 77
  • 2
  • 10

1 Answers1

0

Everywhere is using ExpressJS for this issue because we need a web server which can listen to the calls from network (Local or internet) and make it understandable in server side. You ran a http server on your own, but you need to handle request which are coming from clientside web. Due to your purpose, we need express too. Express also uses http library for creating a web server so you don't need to get worried about all that stuff. I don't know your problem with express but i suggest using express and its recommended approaches for solving this issue.


Update

This topic will help you for creating a web server just with Node.js: Node.js server that accepts POST requests

Create a file called server.js and use above answer to create http server and then run it from a terminal with node app.js command. (Make sure that your terminal is in right directory)

Put your database related code in that section which accepts request under POST method.

Then create a javascript file for your HTML page and link it as you know with <script src="">... and simply use fetch API for sending request that http server that you just ran. Read about fetch here.

Alireza Kiani
  • 410
  • 1
  • 5
  • 11
  • 1
    Hi, Can you tell me how I can handle requests using Node. I would like to take the longer route by exploring just the NodeJS part without Express, if you don't mind. Any resources you could lead me to would be really helpful, thanks. – Remix Protocol Aug 02 '20 at 14:36
  • 1
    Wow, THANKS! I think this will do! – Remix Protocol Aug 02 '20 at 15:22