List

Tools yang digunakan

Visual Studio Code – https://code.visualstudio.com/Download
– enable extension Node.js Module Intellisense
– enable extension npm Intellisense
Insomnia – https://insomnia.rest/download

1. Create directory

#mkdir nodejs-demo

2. Initialize package dalam directory tu

#npm init

 

install keperluan untuk app
untuk oracle

#npm i –save express oracledb knex body-parser cookie-parser

untuk mysql

#npm i –save express knex body-parser cookie-parser mysql

nak install kemudian mysql, boleh run

#npm i –save mysql

install nodemon utk handle auto start stop service bila ada changes kt skrip node js

#npm i -D nodemon

tambah script utk handle run start

“scripts”: {
“dev”: “nodemon -q -w \”.\” –ext \”.\” -i \”node_modules\” –exec \” npm run start\” “,
“test”: “echo \”Error: no test specified\” && exit 1″
},
tambah start script
“scripts”: {
“start”: “node app.js –verbose”,
“dev”: “nodemon -q -w \”.\” –ext \”.\” -i \”node_modules\” –exec \” npm run start\” “,
“test”: “echo \”Error: no test specified\” && exit 1″
},
create file app.js
const express = require(“express”);
const app = express();
app.get(‘/’, (req, res) => {
res.send(“UMP API”);
});
app.listen(3000, (err) => {
if(err) console.error(err);
elseconsole.log(“Server running at localhost:3000”);
});
ubah skrip utk hantar status dlm bentuk json
const express = require(“express”);
const app = express();
app.get(‘/’, (req, res) => {
//res.send(“UMP API”);
res.status(200).json({
status:’OK’,
message:’Welcome to UMP API’,
time:newDate().toISOString()
});
});
app.listen(3000, (err) => {
if(err) console.error(err);
elseconsole.log(“Server running at localhost:3000”);
});
Guna middleware bodyParser untuk handle POST
tambah skrip

Leave a Reply

Your email address will not be published. Required fields are marked *

  Posts

1 2
September 20th, 2021

FortiClient VPN 7 with MacOS Big Sur 11 Issue [SOLVED]

January 6th, 2021

Drupal 8 – PHPMailer SMTP

June 23rd, 2020

Drupal 8 – Setup composer dalam macOS

June 15th, 2020

Flutter (Mac OS) – Penyediaan Persekitaran

March 14th, 2019

Drupal 8 – Composer, Drush & Git

January 3rd, 2019

Node JS – Authentication using JSON Web Token (JWT)

January 3rd, 2019

Node JS – Set config ENV (dotenv)

January 3rd, 2019

Cara setup dan guna GitHub

January 2nd, 2019

Node JS – Connect to database Oracle with knex

January 2nd, 2019

Node Js – Training 2