Posts

Showing posts with the label node.js express

How to Use AJAX with Node.js, Express, and MongoDB

Project Structure |-------------- models |-------------- task.js |-------------- public |-------------- data.js |-------------- routes |-------------- taskroute.js |-------------- views |-------------- demo.ejs |-------------- app.js |-------------- package.json Setup The Folder To create a folder, open your command prompt and type: mkdir ajax Change to the folder by typing: cd ajax Setup Node In Folder On the console, initialize your Node.js project: npm init -y This creates a package.json file indicating that Node is initialized in the folder. Install Packages Install the required packages for building the application: npm install body-parser express ejs mongoose jquery Models Create a new folder named models , add a file named task.js , and insert the following code: var mongoose = require('mongoose'); var taskSchema = new mongoose.Schema({ task:{ ...

How to Apply Form Validation in Node.js (Express)

Image
Image by Gerd Altmann from Pixabay Open the console and create a new folder named validations by typing command mkdir followed by folder name. Change to the new folder by typing command cd validations.  Now setup node project by typing npm init. This will auto generate a package.json file in folder. now add a add.js file and then open the package.json and in " scripts" write "start":"app.js". The package.json will look like this.  Now type npm install followed by package name. We install the following packages: express ejs body-parser express-validator. Now add a folder name views . And in it make a new file names register.ejs. app.js var express = require ( 'express' ); var bodyParser = require ( 'body-parser' ); var path = require ( 'path' ) var {check,validationResult} = require ( 'express-val...