I have developed an application on Ionic 2 and Angular2 with MongoDB as database and Node.js and Express.js as a server.I have created databse on my laptop as mongoose.connect('mongodb://localhost:27017/db')
.
Now, I have launched an AWS EC2 Linux instance and installed node and npm packages.
Successfully cloned my application with git repo in EC2 Linux instance and installed mongodb server and connected to the shell of my laptop's mongoDB by commenting bindip
in mongod.conf
file.
Now when I am running node server.js
in the EC2 instance is reads the console.log(App listining to port 8080')
which is present just below app.listen(8080)
in server.js file.After that nothing gets happened.
1.)How can I connect the server mongodb to the db that I have created on my laptop with the application as localhost, i.e mongoose.connect('mongodb://localhost:27017/db')
.
For Reference I am attaching my server.js file
server.js
var express = require("express");
var app = express();
var mongoose = require('mongoose');
var morgan = require('morgan');
var bodyParser = require('body-parser');
var cors = require('cors');
var methodOverride = require('method-override');
mongoose.connect('mongodb://localhost/reviewking');
app.use(morgan('dev')); // log every request to the console
app.use(bodyParser.urlencoded({ 'extended': 'true' })); // parse application/x-www-form-urlencoded
app.use(bodyParser.json()); // parse application/json
app.use(bodyParser.json({ type: 'application/vnd.api+json' })); // parse application/vnd.api+json as json
app.use(methodOverride());
app.use(cors());
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-control-Allow-Method", "DELETE,PUT");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
//Models
var Review = mongoose.model('Review', {
title: String,
description: String,
rating: Number
});
//Routes
app.get('/api/reviews', function (req, res) {
console.log('fetching reviews..');
Review.find(function (err, reviews) {
if (err)
res.send(err);
res.json(reviews);
});
});
app.post('/api/reviews',function(req,res){
console.log('creating reviews');
//create a review
Review.create({
title: req.body.title,
description:req.body.description,
rating:req.body.rating,
done:false
}, function(err, reviews){
if(err)
res.send(err);
Review.find(function(err,reviews){
if(err)
res.send(err);
res.json(reviews);
});
});
});
app.listen(8080);
console.log('app listening on port 8080')
via Aditya Jain
No comments:
Post a Comment