Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
57 views
in Technique[技术] by (71.8m points)

javascript - How to host node js server on an ec2 instance

I am attempting to deploy a full stack MERN CRUD stack (M being mysql) to AWS. I have successfully hosted mysql database on an RDS instance. My next step is to deploy my node/express server to AWS. I got advise to use ec2 to create a virtual machine on which to host this node backend. I have successfully created the ec2 instance. However, I am at a loss as to how to proceed. To summarize, I am trying to host my node/express server on AWS using an ec2 virtual machine. I just can't seem to figure out the next steps (after creating the initial ec2 instance). In case the context would help, here is my node/express server. Any tips, resources, suggestions, or knowledge you can provide me sure would be appreciated!!

const express = require("express");
const mysql = require("mysql");
const cors = require("cors")
const formData = require("express-form-data");

var app = express();
app.use(cors());
app.use(formData.parse({maxFieldSize: '10mb'}));

var mysqlConnection = mysql.createConnection({
    host: ****,
    user: ****,
    password: ****,
    database: ****
})

mysqlConnection.connect((err) => {
    if(err) {
        throw err;
    }
    console.log("Connected to AWS!")
})

app.get('/', (request, response) => {
    response.send('hello from /')
});

app.post('/createUser', function(req, res) {
    console.log('/createUser')
    console.log(req.body);
    const createUser = `CALL CreateUser('${req.body.username}', '${req.body.password}', 
'${req.body.firstName}')`;
    mysqlConnection.query(createUser, (error, result) => {
        if (error) {
            console.log(error);
        } else {
            console.log('created user');
        }
    })
});

app.get('/signIn', function (request, response) {
    console.log('in /signIn');
    console.log(request.query);
    var {username, password} = request.query;
    console.log(username)
    console.log(password)
    var validateAccount = `CALL ValidateAccount('${username}', '${password}')`;
    var test = `SELECT UserID FROM users WHERE Username = '${username}' AND Passcode = 
    '${password}'`;

    mysqlConnection.query(validateAccount, (error, result) => {
        if (error) {
            console.log(error);
        } else if (result[0] === undefined || result[0].length === 0) {
            console.log('user does not exist');
        } else {
            console.log('user exists');
            console.log(result[0][0].UserID);
            response.status(200).json({
                userID: result[0][0].UserID,
                username: result[0][0].Username,
                passcode: result[0][0].Passcode,
                employmentStatus: result[0][0].EmploymentStatus
            })
        }
    })
})

app.listen(5000, function() {
    console.log("Listening on port 5000");
});
question from:https://stackoverflow.com/questions/65867255/how-to-host-node-js-server-on-an-ec2-instance

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Since you have already created an instance so it must have generated a pem file. So I will tell you steps after generating .pem file.

  1. pem file is private key file so you store it in somewhere safe place.
  2. open cmd and navigate to directory where you store the pem file.
  3. in cmd type ssh -i file_name.pem ubuntu@public IP you can find the public ip in the instance description.
  4. so you will get a virtual OS cmd that you have selected. Now you can pull your code directly from GIT or use the FTP such as filezilla to manually upload the file.
  5. you can read about connecting to filezilla here.https://forums.aws.amazon.com/thread.jspa?messageID=741338

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...