-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
138 lines (108 loc) · 2.77 KB
/
app.js
File metadata and controls
138 lines (108 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/**
*
* ExpressJS Tutorial - Komunitas Programmer Makassar
*
* Facebook Group : https://www.facebook.com/groups/ProgrammerMakassar/
*
* ditulis oleh @ri7nz
*
*/
/**
* Deklarasi variable
*
* var express diberi nilai dari lib expressJs
* var app diberi nilai dari express constructor
* var path diberi nilai dari lib path
*
*/
var express = require('express');
var app = express();
var path = require('path');
const public_html = __dirname + '/public';
/**
*
* deklarsi static folder/path
*
*/
app.use('/static', express.static( public_html + '/static' ) );
/**
*
* Membuat route
*
* app.get( uri, function(request,response) );
*
* app.get => fungsi yang memiliki 2 parameter
*
* parameter pertama yakni uri dengan nilai string
*
* parameter kedua dengan nilai function
*
* yg dimana parameter kedua memiliki 2 parameter
*
* didalamnya yaitu parameter pertama untuk mengambil
*
* nilai request dari client dan parameter kedua
*
* memberikan response kepada client
*
*/
/**
* contoh pada fungsi ini kita akan mengambil nilai dan memberikan
*
* response dari root uri atau '/'
*
* '/' ==> http://alamatweb.domain/
*
* jadi parameter pertama diberi nilai '/'
*
* parameter kedua mengerjakan fungsi atau logic
*
*/
app.get('/', (req, res) => {
/*
* res => response
*
* res.sendFile => memberikan response kepada client berupa file/document
*
* path.join( __dirname + files ) => mengambungkan alamat/path directory
*
* __dirname => lokasi project app.js
*
* files => files/document yang diberikan kepada client
*
*/
return res.sendFile( path.join( public_html + '/index.html') );
});
/**
* http://alamat/contact
*/
app.get('/contact', (req, res) => {
return res.sendFile( path.join( public_html + '/contact.html') );
});
/**
* http://alamat/about
*/
app.get('/about', (req, res) => {
return res.sendFile( path.join( public_html + '/about.html') );
});
/**
* http://alamat/render
*/
app.get('/render', (req, res) => {
/**
* res.send( nilai ) => hampir sama dengan fungsi res.sendFile hanya saja fungsi ini untuk
* mengirim nilainya secara langsung kepada client
*/
let html ='<html> <title>Lewat Var HTML</title> <body><p> Hello Daeng, di cetak di <i>let html</i> <br> Kembali <a href="/">Ke Halaman Utama</a> .</p> </body></html>';
return res.send( html );
});
/**
* http://alamat/react
*/
app.get('/react', (req, res) => {
return res.sendFile( path.join( public_html + '/react.html') );
});
var port = 3000;
app.listen( port , ()=> {
console.log('Applikasi Berjalan di http://localhost:' + port + '/' );
});