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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…