Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- TO SETUP NODEJS --EXPRESS ON Ubuntu EC2 Instance
- 1. Create Ubuntu 18.04 EC2 instance as:
- Ubuntu Server 18.04 LTS (HVM), SSD Volume Type - ami-0ac019f4fcb7cb7e6 (64-bit x86) /
- Why Ubuntu? --This is the free Linux distro which is loaded with the latest tools and updated repository. So most of the things work smoothly here.
- Select this specific 18.04 version.
- 2. Connect to ubuntu instance.
- To connect to the ec2 instance we have two methods:
- Using putty if on windows.
- Using direct ssh command if we are on linux
- Syntax for direct ssh is
- ssh -i "deepak.pem" ubuntu@ec2-52-14-252-174.us-east-2.compute.amazonaws.com
- 3. Once Connected use below commands:
- # sudo -s --To enter into the root mode.
- # apt update --To update the repository
- # apt upgrade --To upgrade the upgradable packages if any.
- #apt install nodejs --To install nodejs
- # apt install npm --To Install NodeJS
- # mkdir code --Create Directory Code
- # cd code --To enter into the code folder
- # npm install express-generator -g --save --To install express
- This below command will create files inside the code folder.
- # express -v hbs --To install hbs module in the code directory
- # npm install --To install dependencies.
- # npm install nodemon -g --save --To install nodemon(May Not work)
- # npm install mysql --save --To install mysql for npm
- *Check Package.json using “cat Package.json” whether mysql is listed in there or not.
- # npm start --To check whether express server is running or not
- Now from the aws console -->Services-->EC2 instances→ select your instance →
- Then from the description click on the link(launch-wizard-x) in front of the Security Groups and then set the inbound rules:
- 8
- Click on Edit and choose All Traffic and then save.
- **Note: To check whether the npm is ru⅞nning properly copy your public-dns or public ip and put that in your browser as
- http://3.85.52.152:3000/
- This should display Welcome Express page.
- Copy Files from windows to EC2 Instance
- pscp -i "D:\Tools\AWS\amzskd.ppk" "D:\Tools\AWS\yourfile.txt" "ubuntu@3.85.52.152:"
- Copy Files from Linux to EC2 Instance
- Command to copy files from Linux to Amazon instance:
- # scp -r -i "amzskd.pem" codeskd/ ec2-user@18.234.49.255:
- This will copy the file to your home directory.
- ///////////////////////////////////////////////TO PREPARE MYSQL DB SERVER ON Ubuntu EC2 Instance/////////////////
- #apt install mysql-server
- #service mysql start
- # mysql
- MySql Commands:
- >show databases;
- >GRANT ALL PRIVILEGES ON *.* TO 'deepak'@'localhost' IDENTIFIED BY 'deepak123';
- > Ctrl-C -- exit!
- # mysql -u deepak -p hit -- Enter and enter password as provided in the above command <deepak123> in this case.
- > create database node;
- > use node;
- > create table users(ID int AUTO_INCREMENT, Username varchar(20) not null, Password varchar(20) not null, PRIMARY KEY(ID));
- >insert into users(Username, Password) values ('admin', 'admin');
- > select * from users;
- > alter table users add(Firstname varchar(20) not null, Lastname varchar(20) not null, Occupation varchar(20) not null);
- >alter table users add(Email varchar(50) not null);
- > update users set Firstname = 'Shanti Kumar', Lastname = 'Deepak', Occupation='Student', Email='shanti.18704@sscbs.du.ac.in';
- More NPM commands:
- To setup PM2:
- #pm2 start app.jscode# npm install pm2 –g
- REST is just an Architecture.. Widely used for easy to handle api creation.
- As per the REST (REpresentational “State” Transfer) architecture, the server does not store any state about the client session on the server side. This restriction is called Statelessness. Each request from the client to server must contain all of the information necessary to understand the request, and cannot take advantage of any stored context on the server. Session state is therefore kept entirely on the client. client is responsible for storing and handling all application state related information on client side.
- Link to above explanation and other info-->https://restfulapi.net/statelessness/
- Read about express module from below link
- https://expressjs.com/en/guide/routing.html
- create a file with basic hello world output and save it in the router folder inside the code folder: code/router/index2.js
- var express = require('express')
- var app = express.Router()
- // respond with "hello world" when a GET request is made to the homepage
- app.get('/', function (req, res)
- {
- res.send('hello world')
- } )
- module.exports = app
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- make config folder in code aur usme database.js nam se file banani hai>>>>>>
- database.js
- ===========
- ar DataBase =
- {
- host: 'localhost',
- database: 'Node',
- user: 'anu',
- password: 'password'
- };
- module.exports = DataBase;
- \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
- routes folder me hai>>>>>
- index.js
- =======
- var express = require('express');
- var router = express.Router();
- var DataBase = require('../config/database.js');
- var mysql = require('mysql');
- var con = mysql.createConnection(DataBase);
- /* GET home page. */
- router.get('/', function(req, res, next)
- {res.render('login');
- });
- router.post('/userValid', function(req, res, next){
- console.log(req.body)
- data = req.body;
- var username=data["uname"];
- var password=data["psw"];
- con.connect(function(err)
- {
- con.query('select * from Users1 where username=? and password=?',[username,password],function(err,result)
- {
- if(err) throw err;
- if(result.length>0)
- {
- firstname=[]
- lastname=[]
- ocType=[]
- finl=[]
- for(let index=0;index<result.length;index++)
- {
- temp=[]
- temp.push(result[index]["firstname"])
- temp.push(result[index]["lastname"])
- temp.push(result[index]["ocType"])
- finl.push(temp);
- }
- //res.send(result)
- res.render('Dashboard',{finl})
- console.log(finl);
- }
- else
- {
- res.render('login');
- }
- })
- })
- //res.send("Page Working");
- });
- module.exports = router;
- \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
- views folder me file banani hai is nam se>>>>>>
- dashboard
- =========
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <link href="https://cdn.datatables.net/1.10.19/css/dataTables.bootstrap4.min.css" rel="stylesheet" type="text/css">
- <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.1/css/bootstrap.css" rel="stylesheet" type="text/css">
- <script href=https://code.jquery.com/jquery-3.3.1.js"></script>
- <script href=https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
- <script href=https://cdn.datatables.net/1.10.19/js/dataTables.bootstrap4.min.js"></script>
- <title>Page</title>
- </head>
- <body>
- <div id="test" class="container-fluid">
- <h1 class="text-danger" >
- WELCOME TO THE DASHBOARD-
- <small class="text-muted">{{App}}</small>
- </h1>
- <div id="Fail">
- <table id="example" class="table table-striped table-bordered" style="width:100%">
- <thead>
- <th>FirstName</th>
- <th>LastName</th>
- <th>OccupationType</th>
- </thead>
- <tbody>
- {{#each finl}}
- <tr>
- {{#each this}}
- <td>{{this}}</td>
- {{/each}}
- </tr>
- {{/each}}
- </tbody>
- </table>
- </div>
- </div>
- <script type="text/javascript">
- $(document).ready(function() {
- $('#example').DataTable();
- } );</script>
- </body>
- </html>
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- view folder>>>>>>>
- login.hbs
- =========
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>Login</title>
- <link rel="stylesheet" href="style.css" />
- </head>
- <body>
- <div class="form">
- <h1>Log In</h1>
- <form action="/userValid" method="POST" name="login">
- <input type="text" name="username" placeholder="Username" required />
- <input type="password" name="password" placeholder="Password" required />
- <input name="submit" type="submit" value="Login" />
- </form>
- <p>Not registered yet? <a href='registration.php'>Register Here</a></p>
- </div>
- </body>
- </html>
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- view folder>>>>>>
- style.css
- =========
- body {
- font-family:Arial, Sans-Serif;
- }
- .clearfix:before, .clearfix:after{
- content: "";
- display: table;
- }
- .clearfix:after{
- clear: both;
- }
- a{
- color:#0067ab;
- text-decoration:none;
- }
- a:hover{
- text-decoration:underline;
- }
- .form{
- width: 300px;
- margin: 0 auto;
- }
- input[type='text'], input[type='email'],
- input[type='password'] {
- width: 200px;
- border-radius: 2px;
- border: 1px solid #CCC;
- padding: 10px;
- color: #333;
- font-size: 14px;
- margin-top: 10px;
- }
- input[type='submit']{
- padding: 10px 25px 8px;
- color: #fff;
- background-color: #0067ab;
- text-shadow: rgba(0,0,0,0.24) 0 1px 0;
- font-size: 16px;
- box-shadow: rgba(255,255,255,0.24) 0 2px 0 0 inset,#fff 0 1px 0 0;
- border: 1px solid #0164a5;
- border-radius: 2px;
- margin-top: 10px;
- cursor:pointer;
- }
- input[type='submit']:hover {
- background-color: #024978;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement