Node js and backend for web development

let fs=require('fs');

let http=require('http');

let express=require('express');

let mongoose=require('mongoose');

let bodyParser= require('body-parser');

let bp=bodyParser.json();

let app=express();

mongoose.connect('mongodb://admin:[email protected]:47330/boutique');

let animalSchema=new mongoose.Schema({

"animal":String,

"age":Number,

"gender":String

});

let animalModel=new mongoose.model("animal",animalSchema);

// app.get("/",function(req,res){

// let data={

// "animal":"Karthi",

// "age":23,

// "gender":"male"

// }

// animalModel(data).save(function(err,data){

// console.log(err);

// console.log(data);

// });

// });




 

app.use(function(req, res, next) {

res.header("Access-Control-Allow-Origin", "*");

res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");

next();

});

app.get("/animals",function(req,res){

animalModel.find({},function(err,data){

res.json(data);

});

});

app.post("/addAnimal",bp,function(req,res){

animalModel(req.body).save(function(err,data){

console.log(err);

})

});

app.get("/employees",function(req,res){

res.writeHead(200,{'content-Type':'application/json'});

let myReadStream=fs.createReadStream(__dirname+"/data/emp.json","utf-8");

myReadStream.pipe(res);

});



 

//let myWrite=fs.createWriteStream(__dirname+"/data/dataop.json");

/*

let server=http.createServer(function(req,res){

res.writeHead(200,{'content-Type':'application/json'})

let myReadStream=fs.createReadStream(__dirname+"/data/data.json","utf-8");

myReadStream.pipe(res);

});

*/

app.listen(3000);//launching server in the port 3000

console.log("server running @ 3000");

//myReadStream.on('data',function(response){

// console.log(response);

//});

//myReadStream.pipe(myWrite);//copy it to another file


next page for app.js

var app = angular.module("myApp",["ngRoute"]);

app.config(function($routeProvider){

$routeProvider.when("/",{

templateUrl:"template/aboutus.html"

}).when("/animals",{

templateUrl:"template/animalList.html"

}).when("/about",{

templateUrl:"template/aboutus.html"

}).when("/addAnimal",{

templateUrl:"template/addAnimal.html"

});

});

app.controller("myController",function($scope,$http){

$scope.title="welcome";

$http.get("http://localhost:3000/animals").then(function(response){

$scope.data=response.data;

});

});

app.controller("animalController",function($scope,$http)

{

$scope.add=function(){

let data={

"animal":$scope.animalName,

"age":$scope.animalAge,

"gender":$scope.animalGender

}

$http.post("http://localhost:3000/addAnimal",data).then(function(res){

console.log("post request sent");

});

console.log(data);

}

})

Posted on by