Introduction
In this blog, we see how to use Ajax in Node.
Project Structure
|-------------- models
| |-------------- task.js
|
|-------------- public
| |-------------- data.js
|
|--------------- routes
| |-------------- taskroute.js
|
|--------------- views
| |-------------- demo.ejs
|
|--------------- app.js
Setup The Folder
To create a folder, open the command prompt and type cmd mkdir followed by the folder name
# mkdir ajax
Change to the folder by typing the cmd cd followed by the folder name
# cd ajax
Setup Node In Folder
On the console, type the below command
# npm init -y
This will create a package.json file, Which means that node is initialised in the folder.
the package.json will look like this
- {
- "name": "ajax",
- "version": "1.0.0",
- "description": "",
- "main": "index.js",
- "scripts": {
- "test": "echo \"Error: no test specified\" && exit 1"
- },
- "keywords": [],
- "author": "",
- "license": "ISC"
- }
- }
To build application we need to install packages.
To install packages we have to type npm install followed by the package name.
# npm install body-parser express ejs mongoose jquery
After installing packages the package.json file will look like this.
- {
- "name": "ajax",
- "version": "1.0.0",
- "description": "",
- "main": "index.js",
- "scripts": {
- "test": "echo \"Error: no test specified\" && exit 1"
- },
- "keywords": [],
- "author": "",
- "license": "ISC",
- "dependencies": {
- "body-parser": "^1.19.0",
- "ejs": "^3.0.1",
- "express": "^4.17.1",
- "jquery": "^3.4.1",
- "mongoose": "^5.9.2"
- }
- }
Add Folders
We have to add 4 new folders.
- models
- routes
- views
- public
Add new file in this folder and name it task.js
In the task.js file, add the below code.
- task.js
- var mongoose = require('mongoose');
- var taskSchema = new mongoose.Schema({
- task:{
- type:String
- }
- });
- var taskModel = module.exports = mongoose.model('task',taskSchema);
- module.exports.addTask = (task,cb)=>{
- task.save((err,taskData)=>{
- if(err){
- cb(err,null);
- }else{
- cb(null,taskData);
- }
- });
- }
- module.exports.getTask = (cb)=>{
- taskModel.find((err,taskData)=>{
- if(err){
- cb(err,null);
- }else{
- cb(null,taskData);
- }
- });
- }
- module.exports.removeTask = (id,cb)=>{
- taskModel.deleteOne({'_id':id},(err,taskData)=>{
- if(err){
- cb(err,null);
- }else{
- cb(null,taskData);
- }
- });
- }
Add the new file in the folder and name it taskroute.js
In taskroute.js, add below code
- taskroute.js
- var express = require('express');
- var taskModel = require('../models/task');
- var router = express.Router();
- router.get('/home',(req,res)=>{
- res.render('demo');
- });
- router.post('/addtask',(req,res)=>{
- var taskk = new taskModel({
- task:req.body.task
- });
- taskModel.addTask(taskk,(err,taskData)=>{
- if(err){
- res.json({msg:'error'});
- }else{
- res.json({msg:'success'});
- }
- });
- });
- router.get('/gettask',(req,res)=>{
- taskModel.getTask((err,taskData)=>{
- if(err){
- res.json({msg:'error'});
- }else{
- res.json({msg:'success',data:taskData});
- }
- });
- });
- router.delete('/removetask',(req,res)=>{
- taskModel.removeTask(req.body.id,(err,taskData)=>{
- if(err){
- res.json({msg:'error'});
- }else{
- res.json({msg:'success'});
- }
- });
- });
- module.exports = router;
Add new file and name it demo.ejs
Rushi MehtaPosted Feb 26, 2020, 2:05 AM
Thanks for sharing