简体   繁体   中英

How to host my application (MEAN) on amazon EC2 Linux instance?

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 database 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, ie 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')

Probably the easiest way is to run mongodump , upload the dump to your server (probably use scp ) and restore using mongorestore .

# Backup the reviewking database (on your laptop)
mongodump --db reviewking

# Restore the reviewking database (on your server)
mongorestore --db reviewking dump/reviewking

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM