|
 |
|
มือใหม่หัดเขียน NodeJs ผลัดมาตั้งนาน กว่าจะได้โอกาส |
|
 |
|
|
 |
 |
|
เพิ่งจะได้โอกาสลง โปรแกรม และทดลองเขียน โมฯแบบ เบๆ เลย แบบ ยังอ่านตัวอย่างไม่กี่หน้า
ลง nodejs บน Centos7
login ด้วย root
คำสั่งแรก
yum update -y
คำสั่งที่ 2
curl -sL https://rpm.nodesource.com/setup_10.x | bash -
คำสั่งที่ 3
yum install nodejs
คำสั่งที่ 4
firewall-cmd --add-port=8080/tcp --zone=public --permanent
คำสั่งที่ 5
firewall-cmd --reload
ต่อไปก็ สร้าง folder มาซักอัน แล้วย้ายเข้าไปในนั้น cd xxxxx
สร้างไฟล์ icon มาใช้งานด้วยนะครับ
favicon.ico
อย่าลืมนะครับเดี๋ยวโปรแกรม error นะครับ เพราะไม่ได้ตรวจเช็คอะไรเลย 55555
สร้างไฟล์ ทดสอบ node1.js
Code (JavaScript)
var http = require('http'), fs = require('fs');//, favicon = require('serve-favicon');
const favicon = fs.readFileSync('favicon.ico'); // read file
http.createServer(function (req, res) {
var str = req.url.split('/');
switch(str[1]){
case 'favicon.ico':
res.statusCode = 200;
res.setHeader('Content-Length', favicon.length);
res.setHeader('Content-Type', 'image/x-icon');
res.setHeader("Cache-Control", "public, max-age=2592000"); // expiers after a month
res.setHeader("Expires", new Date(Date.now() + 2592000000).toUTCString());
res.end(favicon);
break;
case 'Home':
res.writeHead(200, {'Content-Type': 'text/html'});
res.write('<!doctype html><html><head><title>Chaidhanan</title></head><body>TEST NodeJs</body></html>');
res.end();
break;
default:
console.log(str);
res.end();
}
}).listen(8080);
คำสั่งเปิดโปรแกรม
node node1.js
ทดสอบด้วย browser http:\\192.168.1.21:8080/Home
เมื่ออ่านเรื่อง session เสร็จจะมาโพสต่อ 555555
Tag : Node.js, Linux

|
ประวัติการแก้ไข 2020-07-31 17:32:34
|
 |
 |
 |
 |
Date :
2020-07-31 17:29:12 |
By :
Chaidhanan |
View :
1226 |
Reply :
3 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ปูเสื่อนอนรอ อาจได้กลับมาทบทวนความรู้ในภายภาคหน้า
|
 |
 |
 |
 |
Date :
2020-08-03 14:04:52 |
By :
PhrayaDev |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ก่อน ที่จะไป session ขอต่อด้วย https ก่อนนะครับ พอดี isp ที่บ้าน เป็น AIS เขาให้ใช้ https: แบบ auto ไม่ใช้ไม่ได้
ต้องขอบอกนะครับ ผมเขียนไปด้วย อ่าน document ไปด้วยนะครับ 55555 ก็จาก topic มือใหม่หัดเขียน nodejs 55555
อาจจะวนไปวนมา บ้างนะครับ
################################
เริ่มต้นก็ต้องเตรียมไฟล์ certificate ก่อน ผมใช้บริการของเวปนี้ ฟรีทุก 3 เดือน ครบ 3 เดือน เข้าไปยกเลิก ของเก่าแล้ว สร้าง ใหม่ 5555
https://www.sslforfree.com/
ค่อย ไล่ไปที่ละเสตป ใช้การสร้าง ด้วย http download นะครับ (feature สุดท้าย)
เราจะได้ ไฟล์ มา 3 ไฟล์ (ใช้แค่ 2)
ca_bundle.crt -- ไม่ใช้
certificate.crt
private.key
นำไฟล์ ที่ได้ไปใส่ใน folder ของ project
จบเรื่อง สร้างไฟล์ certificate
################################
ตัวอย่าง โปรแกรม
Code (JavaScript)
const express = require('express');
const session = require('express-session');
const bodyParser = require('body-parser');
const router = express.Router();
var https = require('https'), fs = require('fs');
var port = process.env.PORT || 443;
const options = {
key: fs.readFileSync('../Certificate/private.key'),
cert: fs.readFileSync('../Certificate/certificate.crt')
};
const favicon = fs.readFileSync('../favicon.ico'); // read file
const app = express();
//app.use(session({secret: 'ssshhhhh',saveUninitialized: true,resave: true}));
app.use(session({secret: 'HKEncript', saveUninitialized: true, resave: true}));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static(__dirname + '/views'));
var sess; // global session, NOT recommended
router.get('/',(req,res) => {
sess = req.session;
if(sess.email) {
return res.redirect('/admin');
}
res.sendFile('index.html');
});
router.post('/login',(req,res) => {
sess = req.session;
sess.email = req.body.email;
res.end('done');
});
router.get('/favicon.ico',(req,res) => {
res.statusCode = 200;
res.setHeader('Content-Length', favicon.length);
res.setHeader('Content-Type', 'image/x-icon');
res.setHeader("Cache-Control", "public, max-age=2592000"); // expiers after a month
res.setHeader("Expires", new Date(Date.now() + 2592000000).toUTCString());
res.end(favicon);
});
router.get('/admin',(req,res) => {
sess = req.session;
if(sess.email) {
res.write(`<h1>Hello ${sess.email} </h1><br>`);
res.end('<a href='+'/logout'+'>Logout</a>');
}
else {
res.write('<h1>Please login first.</h1>');
res.end('<a href='+'/'+'>Login</a>');
}
});
router.get('/logout',(req,res) => {
req.session.destroy((err) => {
if(err) {
return console.log(err);
}
res.redirect('/');
});
});
app.use('/', router);
var server = https.createServer(options, app);
server.listen( port, function () {
console.log( 'Hello Web Server listening on port ' + server.address().port );
});
|
ประวัติการแก้ไข 2020-08-03 14:49:04
 |
 |
 |
 |
Date :
2020-08-03 14:43:15 |
By :
Chaidhanan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 00
|