Monday, May 27, 2019

NodeJS / Express: what is “app.use”?

NodeJS / Express: what is “app.use”?

In the docs for the NodeJS express module, the example code has app.use(...).
What is the use function and where is it defined?
                                                                                 --question from stack overflow--

Answer:-
       According to the Node and express there is something called middleware functions.That means that functions have to access to the request and response objects in the application.Using this middleware functions can execute any code,end the request and response life cycle and also can all to the next middleware.I explain this concept using following example.

 books.js
const mongoose=require('mongoose');
const user=mongoose.Schema;
 let users=new user({
  title:{
         type:String 
     },
     auther:{
         type:String   
 }
 });
 module.exports=mongoose.model('users',users);

book.js
const express=require('express');
const mongoose=require('mongoose');
const cors=require('cors');
const app=express();
const bodyParser=require('body-parser');
const todoRouts=express.Router();

app.use(bodyParser.json());
app.use(cors());
let todo=require('./books');
mongoose.connect('mongodb://127.0.0.1:27017/order',{useNewUrlParser:true})
const connection=mongoose.connection;
 connection.once('open',function(){
    console.log('connected');
})

todoRouts.route('/add').post(function(req,res){
    let to=new todo(req.body);
    to.save().then(to=>{
        res.status(200).json({'todo':'succsessfully'});
    }).catch(err=>{
        res.status(400).send('fail');
    })
})
 app.use('/todo',todoRouts)

app.listen(3000,function(){
    console.log('server connect');
})
 
According to this example,
const app=express() represents the create and object using express.
const todoRouts=express.Router() use to create an object with routing the books.js 
class to get its defined schema. 
app.use() function specify the middleware function.Things that inside the app.use()
load that things before the get,post and other http requests.
 app.use(bodyParser.json()) , app.use(cors()) and app.use('/todo',todoRouts)
These parts load before. 
 When we use Postman to run this application we need to type url as http://localhost:3000/todo/add

Tuesday, May 14, 2019

Experiance doing the application framework group project


Experience doing the application framework group project and critical reflections.
                      


  •      Our 3 rd year group project is about student and instructor information system.there are four members in our group.
  •     Mainly we able to need create logins with authentication and also It will be able to full stack CRUD application.
  •    We need to use react as the frontend,node with expressas the backend and mongodb as the database and also use css for add styles.
  •  React,node,express and mongodb are new technologies for me.So it is a big challenge for me.But this project is a group project .so our members work together and try to find solutions for problems.
  • There are some critical problems I faced when work with this full stack application.When create a node ,react project need to download dependencies.
  • But the critical problem is when there is not proper network connection not properly download them.So not work the project successfully.
  •  Sometimes not work the npm command for download libraries.So I try to 'npm cache clean --force' command to solve that problem.
  •  Node and express use to create the backend.We can use postman and REST client  tools to check the connection and queries are work success.axios use for connect backend and frontend.
       I think this project is help to get complete idea about full stack applications.Not only that this will help to get the experience about team work.