skip to Main Content

The question is very much the same as it is on title, my aboslute doubt about it, is if this is possible ? all i’ve read about server side aplication lead to a DB like SQL or something aside from web.

So i would like to know if is it possible to simple submit something on a textarea and store on a server aplication made by node.js

for example:

var express = require('express');
var bodyParser = require('body-parser');
var app     = express();


//here where i got with some help of others questions on stackoverflow
app.use(bodyParser.json());

//Note that in version 4 of express, express.bodyParser() was
//deprecated in favor of a separate 'body-parser' module.
app.use(bodyParser.urlencoded({ extended: true })); 


app.listen(3000, function() {
  console.log('Server running at http://127.0.0.1:3000/');
})
<div class="Contact_info">
        <h1 id="Resume">Nos aconselhe</h1><!-- Give us some advice form -->
        <form id="formCritic"         action="http://localhost:3000"  method="get"> <!-- this is my localhost created using JS on node.js -->
            <input type="text" id="Name" name="Nome" placeholder="Seu nome.." >
            <textarea class="text_area_submit" id="Questio" name="Questio" placeholder="Nos informe qualquer duvida ou problema.." ></textarea>
<!-- here is the text area wich i would like to store what users input on my localhost -->

            <input id="submit_text_are" type="submit" value="Submit">
          
        </form>
</div>

Just some extra info, i have my root directory, named : Myproject

inside it there are 5 folders + Index.html

one folder name is JS, wich relie my js program to my html and my serverSide.js made by node.js .

Im add this information, because i’ve arealdy see alot of answers on some questions here that might treat this matter about where your folder wich you’ve created your serverSide with node.js important.

2

Answers


  1. is it possible to simple submit something on a textarea and store on a server aplication made by node.js

    Yes. But you’re probably misunderstanding a few things.

    You can store data "in the application" in the form of just saving it to a variable in a more global scope, for example. But that data will be very short-lived. It’s not persisted anywhere, so as soon as anything happens to the running application that data will be gone.

    with no SQL or any DB. Just HTML

    You can also write your data to HTML files. Though as a data storage format HTML is very loosely structured and this is going to very quickly become difficult to manage.

    You could store your data in any format you like though. So perhaps serializing to a JSON file would make more sense. It’s persisted outside of the application, so it’ll survive a reboot. It’s in a structured format. That could work for you.

    Though as that data scales to any significant amount, or as the need to read and write the data reaches any higher volumes than just you alone, writing it to the file system becomes less performant and more cumbersome.

    The solution to that is to use a database.

    Login or Signup to reply.
  2. Yes, you can store data in json file, for example. You can see an example here or here.
    But it’s not a great idea for a real application.

    For your case something like this should work.

    const express = require('express');
    const bodyParser = require('body-parser');
    const fs = require('fs');
    const path = require('path');
    const app = express();
    
    // Middleware to parse JSON and urlencoded data
    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({ extended: true }));
    
    app.post('/saveInput', (req, res) => {
        // Get the message from the request body
        const { Nome, Questio } = req.body;
        console.log( Nome, Questio )
        // Write the message to a JSON file
        const data = JSON.stringify({  Nome, Questio }) + 'n';
        fs.appendFile(path.join(__dirname, 'message.json'), data, (err) => {
            if (err) {
                console.error('Error writing message to file:', err);
                res.status(500).send('Internal Server Error');
            } else {
                console.log('Message written to file');
                res.status(200).send('Message written to file');
            }
        });
    });
    
    // Start the server
    const port = 3000;
    app.listen(port, () => {
        console.log(`Server running at http://127.0.0.1:${port}/`);
    });
    <script>
    document.getElementById('formCritic').addEventListener('submit', function(event) {
        event.preventDefault(); // Prevent the default form submission behavior
    
        const formData = new FormData(this);
        fetch('http://localhost:3000/saveInput', {
            method: 'POST',
            body: formData
        })
    });
    
    
    </script>
    
    <div class="Contact_info">
        <h1 id="Resume">Nos aconselhe</h1><!-- Give us some advice form -->
        <form id="formCritic" action="http://localhost:3000/saveInput" method="post"> <!-- Specify the POST method and endpoint -->
            <input type="text" id="Name" name="Nome" placeholder="Seu nome.." >
            <textarea class="text_area_submit" id="Questio" name="Questio" placeholder="Nos informe qualquer duvida ou problema.." ></textarea>
            <input id="submit_text_are" type="submit" value="Submit">
        </form>
    </div>

    Just make sure you have a file message.json in the directory.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search