Saving Users\’ Details to a Text file using Node and Express Js

\"users

Saving users details in a text file using node and Expressjs may seem like a daunting task for most beginners in NodeJS, but it really is not as difficult as it sounds. Being able to save your user details from an HTML form on your website might come in handy if there is no database to work with yet during development, so it is just a simple improvision to simulate a database in your web applications for testing and development, but I wouldn\’t recommend it to be used during production; so get yourself a database before deployment.

So for testing purposes and development stages of your website, I\’m going to walk you through doing a simple operation like saving user details to a text file in your local computer directory.

Before you do anything, make sure you have nodejs installed on your computer, preferably the newest version for your Operating system, then create a new project folder in your computer and initialize your node application by typing \’npm init\’ on your command line or terminal.

Creating a users form

After you have initialized your project, it is now time to create a form that will accept users\’ input, but first, go into the \’package.json\’ file in your project directory and put a \”start\” script there, set it to \’node index\’ or \’nodemon index\’\'(if you have installed nodemon globally in your computer). when that is done, open your terminal and install the following packages using npm; install ejs, express, I think that is pretty much it for this particular project. Create a views folder in your root directory and create an ejs file there.

In the ejs file in the views folder, scaffold out an HTML template for the user form; this form will have a field for Username, Email, and Password.

Initializing your express server

Now that you have your ejs form ready, create a file called \”index.js\” in your root directory, this is where our express server will be created to listen for requests. You can require or bring in express in your application by simply typing \”require(\’express\’)\” and saving the value to a variable; you can choose any name for your variable, but \”express\” is the general convention. After that, invoke or call the express library by defining a variable \”app\”, which is the convention, and setting its value to the invoked \”express\” variable, i.e \”const app = express()\”; now to initialize the express server to listen to request on any port of your choice, use the listen express method, \”app.listen(3000, ()=>console.log(\”server started and listening for requests on port 3000\”)\”. This will initialize an express server for you that is listening on port 3000 in your system, in your terminal you should be able to see the \”console.log()\” statement.

Set up Your GET and POST routes

Now, it\’s time to set up our routes or mini-api for our application, first of all, to set up the get route to the ejs file we created earlier (index.ejs file, that renders HTML template), use the app we have defined earlier, set a get route to the index.ejs page by typing in your code editor \”app.get(\’/login\”, res.render(\’index\”)); this will set up a \’/login\’ route to render the ejs template in your views folder. P.S: In order to use ejs as your templating in engine, you must first set ejs as your view engine; \”app.set(\’view engine\”, \”ejs\”); before you can use ejs templates in your application.

Now that we have the GET route handled, we can set up the POST route for when the form is submitted in the frontend (HTML template being rendered to the brower by ejs). Our main objective is to save user inputs to a txt file in our root directory, to achieve this, we need the \”fs\” nodejs module, which stands for \”file system\”, to interact with files in our root directory

Using the fs nodejs module/package to save users details in a txt file

require the \’fs\’ module and save it in a variable/constant \”fs\” at the top of the file, just like the other imports. Create a route \”app.post(\’/login\”, (req, res)=>{}); then go to the top of the file and use the express.urlencoded middleware \”app.use(express.urlencoded({extended: true})\” to recieve post requests from a form and save it into the req.body of th post request. When that is done, go to the empty curly braces in the post route use JSON.stringify method to convert the objects in the request body into json texts(\”const inputs = JSON.stringify(req.body)\”). when that is done use the fs module to append the inputs into a txt file(\”const savedInputs = await fs.appendFile(\”users.txt\”, inputs, ()=>console.log(\’written\”)\”). the await is because this is an asynchronous task; then send out a response to the post request by re-rendering the template(\”res.render(\’login\”);\”)

With that done we have successfully wriiten to a txt file from a form input by a user!!!!

Leave a Comment

Your email address will not be published.

%d bloggers like this: