Advertisement
Guest User

Untitled

a guest
Jan 10th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.94 KB | None | 0 0
  1. TO SETUP NODEJS --EXPRESS ON Ubuntu EC2 Instance
  2.  
  3. 1. Create Ubuntu 18.04 EC2 instance as:
  4. Ubuntu Server 18.04 LTS (HVM), SSD Volume Type - ami-0ac019f4fcb7cb7e6 (64-bit x86) /
  5. 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.
  6.  
  7. Select this specific 18.04 version.
  8.  
  9. 2. Connect to ubuntu instance.
  10. To connect to the ec2 instance we have two methods:
  11. Using putty if on windows.
  12. Using direct ssh command if we are on linux
  13. Syntax for direct ssh is
  14.  
  15. ssh -i "deepak.pem" ubuntu@ec2-52-14-252-174.us-east-2.compute.amazonaws.com
  16.  
  17. 3. Once Connected use below commands:
  18.  
  19. # sudo -s --To enter into the root mode.
  20. # apt update --To update the repository
  21. # apt upgrade --To upgrade the upgradable packages if any.
  22. #apt install nodejs --To install nodejs
  23. # apt install npm --To Install NodeJS
  24. # mkdir code --Create Directory Code
  25. # cd code --To enter into the code folder
  26. # npm install express-generator -g --save --To install express
  27. This below command will create files inside the code folder.
  28. # express -v hbs --To install hbs module in the code directory
  29. # npm install --To install dependencies.
  30. # npm install nodemon -g --save --To install nodemon(May Not work)
  31. # npm install mysql --save --To install mysql for npm
  32. *Check Package.json using “cat Package.json” whether mysql is listed in there or not.
  33. # npm start --To check whether express server is running or not
  34.  
  35. Now from the aws console -->Services-->EC2 instances→ select your instance →
  36. Then from the description click on the link(launch-wizard-x) in front of the Security Groups and then set the inbound rules:
  37.  
  38.  
  39. 8
  40. Click on Edit and choose All Traffic and then save.
  41.  
  42.  
  43. **Note: To check whether the npm is ru⅞nning properly copy your public-dns or public ip and put that in your browser as
  44. http://3.85.52.152:3000/
  45. This should display Welcome Express page.
  46.  
  47.  
  48. Copy Files from windows to EC2 Instance
  49. pscp -i "D:\Tools\AWS\amzskd.ppk" "D:\Tools\AWS\yourfile.txt" "ubuntu@3.85.52.152:"
  50.  
  51.  
  52. Copy Files from Linux to EC2 Instance
  53. Command to copy files from Linux to Amazon instance:
  54. # scp -r -i "amzskd.pem" codeskd/ ec2-user@18.234.49.255:
  55.  
  56. This will copy the file to your home directory.
  57.  
  58.  
  59. ///////////////////////////////////////////////TO PREPARE MYSQL DB SERVER ON Ubuntu EC2 Instance/////////////////
  60.  
  61. #apt install mysql-server
  62. #service mysql start
  63. # mysql
  64.  
  65. MySql Commands:
  66. >show databases;
  67. >GRANT ALL PRIVILEGES ON *.* TO 'deepak'@'localhost' IDENTIFIED BY 'deepak123';
  68. > Ctrl-C -- exit!
  69.  
  70. # mysql -u deepak -p hit -- Enter and enter password as provided in the above command <deepak123> in this case.
  71. > create database node;
  72. > use node;
  73. > create table users(ID int AUTO_INCREMENT, Username varchar(20) not null, Password varchar(20) not null, PRIMARY KEY(ID));
  74.  
  75. >insert into users(Username, Password) values ('admin', 'admin');
  76. > select * from users;
  77. > alter table users add(Firstname varchar(20) not null, Lastname varchar(20) not null, Occupation varchar(20) not null);
  78. >alter table users add(Email varchar(50) not null);
  79. > update users set Firstname = 'Shanti Kumar', Lastname = 'Deepak', Occupation='Student', Email='shanti.18704@sscbs.du.ac.in';
  80.  
  81.  
  82.  
  83. More NPM commands:
  84. To setup PM2:
  85. #pm2 start app.jscode# npm install pm2 –g
  86.  
  87. REST is just an Architecture.. Widely used for easy to handle api creation.
  88.  
  89. 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.
  90.  
  91. Link to above explanation and other info-->https://restfulapi.net/statelessness/
  92.  
  93.  
  94. Read about express module from below link
  95. https://expressjs.com/en/guide/routing.html
  96.  
  97. create a file with basic hello world output and save it in the router folder inside the code folder: code/router/index2.js
  98.  
  99. var express = require('express')
  100. var app = express.Router()
  101.  
  102. // respond with "hello world" when a GET request is made to the homepage
  103. app.get('/', function (req, res)
  104. {
  105. res.send('hello world')
  106. } )
  107.  
  108. module.exports = app
  109.  
  110. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  111.  
  112. make config folder in code aur usme database.js nam se file banani hai>>>>>>
  113.  
  114. database.js
  115. ===========
  116.  
  117. ar DataBase =
  118. {
  119. host: 'localhost',
  120. database: 'Node',
  121. user: 'anu',
  122. password: 'password'
  123. };
  124. module.exports = DataBase;
  125.  
  126. \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  127.  
  128. routes folder me hai>>>>>
  129.  
  130. index.js
  131. =======
  132. var express = require('express');
  133. var router = express.Router();
  134. var DataBase = require('../config/database.js');
  135. var mysql = require('mysql');
  136. var con = mysql.createConnection(DataBase);
  137. /* GET home page. */
  138. router.get('/', function(req, res, next)
  139. {res.render('login');
  140. });
  141.  
  142. router.post('/userValid', function(req, res, next){
  143. console.log(req.body)
  144. data = req.body;
  145. var username=data["uname"];
  146. var password=data["psw"];
  147. con.connect(function(err)
  148. {
  149. con.query('select * from Users1 where username=? and password=?',[username,password],function(err,result)
  150. {
  151. if(err) throw err;
  152. if(result.length>0)
  153. {
  154. firstname=[]
  155. lastname=[]
  156. ocType=[]
  157. finl=[]
  158. for(let index=0;index<result.length;index++)
  159. {
  160. temp=[]
  161. temp.push(result[index]["firstname"])
  162. temp.push(result[index]["lastname"])
  163. temp.push(result[index]["ocType"])
  164. finl.push(temp);
  165. }
  166. //res.send(result)
  167. res.render('Dashboard',{finl})
  168. console.log(finl);
  169. }
  170. else
  171. {
  172. res.render('login');
  173. }
  174. })
  175. })
  176. //res.send("Page Working");
  177. });
  178.  
  179. module.exports = router;
  180.  
  181. \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  182.  
  183. views folder me file banani hai is nam se>>>>>>
  184.  
  185. dashboard
  186. =========
  187.  
  188.  
  189. <!DOCTYPE html>
  190. <html lang="en">
  191.  
  192. <head>
  193. <link href="https://cdn.datatables.net/1.10.19/css/dataTables.bootstrap4.min.css" rel="stylesheet" type="text/css">
  194. <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.1/css/bootstrap.css" rel="stylesheet" type="text/css">
  195.  
  196. <script href=https://code.jquery.com/jquery-3.3.1.js"></script>
  197. <script href=https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
  198. <script href=https://cdn.datatables.net/1.10.19/js/dataTables.bootstrap4.min.js"></script>
  199.  
  200.  
  201. <title>Page</title>
  202. </head>
  203.  
  204. <body>
  205. <div id="test" class="container-fluid">
  206. <h1 class="text-danger" >
  207. WELCOME TO THE DASHBOARD-
  208. <small class="text-muted">{{App}}</small>
  209. </h1>
  210. <div id="Fail">
  211. <table id="example" class="table table-striped table-bordered" style="width:100%">
  212. <thead>
  213.  
  214. <th>FirstName</th>
  215. <th>LastName</th>
  216. <th>OccupationType</th>
  217.  
  218. </thead>
  219. <tbody>
  220. {{#each finl}}
  221. <tr>
  222. {{#each this}}
  223. <td>{{this}}</td>
  224. {{/each}}
  225. </tr>
  226. {{/each}}
  227.  
  228. </tbody>
  229. </table>
  230. </div>
  231. </div>
  232. <script type="text/javascript">
  233. $(document).ready(function() {
  234. $('#example').DataTable();
  235. } );</script>
  236.  
  237. </body>
  238.  
  239. </html>
  240.  
  241. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  242.  
  243. view folder>>>>>>>
  244.  
  245. login.hbs
  246. =========
  247.  
  248. <!DOCTYPE html>
  249. <html>
  250. <head>
  251. <meta charset="utf-8">
  252. <title>Login</title>
  253. <link rel="stylesheet" href="style.css" />
  254. </head>
  255. <body>
  256. <div class="form">
  257. <h1>Log In</h1>
  258. <form action="/userValid" method="POST" name="login">
  259. <input type="text" name="username" placeholder="Username" required />
  260. <input type="password" name="password" placeholder="Password" required />
  261. <input name="submit" type="submit" value="Login" />
  262. </form>
  263. <p>Not registered yet? <a href='registration.php'>Register Here</a></p>
  264. </div>
  265. </body>
  266. </html>
  267.  
  268. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  269.  
  270. view folder>>>>>>
  271.  
  272. style.css
  273. =========
  274.  
  275. body {
  276. font-family:Arial, Sans-Serif;
  277. }
  278. .clearfix:before, .clearfix:after{
  279. content: "";
  280. display: table;
  281. }
  282. .clearfix:after{
  283. clear: both;
  284. }
  285. a{
  286. color:#0067ab;
  287. text-decoration:none;
  288. }
  289. a:hover{
  290. text-decoration:underline;
  291. }
  292. .form{
  293. width: 300px;
  294. margin: 0 auto;
  295. }
  296. input[type='text'], input[type='email'],
  297. input[type='password'] {
  298. width: 200px;
  299. border-radius: 2px;
  300. border: 1px solid #CCC;
  301. padding: 10px;
  302. color: #333;
  303. font-size: 14px;
  304. margin-top: 10px;
  305. }
  306. input[type='submit']{
  307. padding: 10px 25px 8px;
  308. color: #fff;
  309. background-color: #0067ab;
  310. text-shadow: rgba(0,0,0,0.24) 0 1px 0;
  311. font-size: 16px;
  312. box-shadow: rgba(255,255,255,0.24) 0 2px 0 0 inset,#fff 0 1px 0 0;
  313. border: 1px solid #0164a5;
  314. border-radius: 2px;
  315. margin-top: 10px;
  316. cursor:pointer;
  317. }
  318. input[type='submit']:hover {
  319. background-color: #024978;
  320. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement