1. Cài đặt
Download NodeJS
Kiểm tra version nodejs trên máy: cmd + node version
Download Git SCM
2. Tạo project
- Tạo thư mục tên project- Tạo *.js
- Chuột phải vào thư tên project, chọn Git Bash Here
- Gõ node *.js, nhấn Enter
3. Biến
var ten_bien = gia_tri;4. Mảng
var mang = {0,1,2,3,4,5};console.log(mang[0]); // 0
5. Hàm
function tinhtinh(a,b){
return a+b;
}
var x = tinhtong(1,3);
/////////////////////////////////////////////////////
function hello()
{
conslole.log("Hello");
}
function goiHam(fn)
{
fn();
}
goiHam(hello); // Hello
////////////////////////////////////////////////////
var tong = function()
{
console.log("thuongthinh");
}
tong();
6. Modules
// hello.jsvar name = "hello thuongthinh";
module.exports = name;
// module.js
var n = require(./hello.js);
console.log(n); // hello thuongthinh
7. Lập trình hướng đối tượng
// pesion.jsvar persion = {
ho : "ta",
ten : "thuong"
xinchao : function()
{
console.log("hello" + this.ten);
}
}
// Sử dụng
persion.xinchao(); // hello ta
console.log(persion["ten"]); // thuong
// khoahoc.js
function KhoaHoc(ten, hocphi) {
this.Ten = ten;
this.HocPhi = hocphi
}
KhoaHoc.prototype.mota = function()
{
console.log("hello" + this.HocPhi);
}
// Sử dung
var nodejs = new KhoaHoc("NodeJS", 1000);
nodejs.mota(); // Nodejs 1000
8. Buffer
Truyền tải dữ liệu qua internet.// Buffer.js
var buffer = new Buffer("hello","uft-8");
console.log (buffer);
console.log(buffer.toString()); // hello
9. File
var fs = require("fs");var noidung = fs.readFileSync(__dirname + "/danhsach.txt");
console.log(nodung);
10. Cài đặt Express
Module hỗ trợ làm WebServer// package.json
{
"name" : "ten_project",
"version" : "0.0.1",
"private" : "true",
"dependencies" : {
"express" : "*"
}
}
- Chuột phải vào thư mục Project, chọn Git Bash Here
- Gõ: npm install (đọc file package.json để download express về project)
// App.js
var express = require("express");
var app = express();
var server = require("http").createServer(app)
server.listen(3000); // Lắng nghe cổng 3000
// Trả về khi có requet từ client
app.get("/", function(res,res) {
res.send("<font color=red>Hello</font>");
});
// Send file html
app.get("/", function(res,res) {
res.sendFile(__dirname + ./index.html);
});
// Routing
app.get("/gioithieu.aspx", function(res,res) {
res.sendFile(__dirname + ./index.html);
});
// Get parameters
app.get("/gioithieu.aspx/:so1/:so2", function(res,res) {
var n = req.params.so1;
res.send(n);
});
11. Socket IO
// package.json{
"name" : "ten_project",
"version" : "0.0.1",
"private" : "true",
"dependencies" : {
"express" : "*",
"socket.io": "*",
"socketio-file-upload": "^.0.4.4"
}
}
// Server.js
var express = require("express");
var app = express();
var server = require("http").createServer(app);
var io = require("socket.io").listen(server);
var fs = require("fs");
server.listen(process.env.PORT || 3000);
io.sockets.on('connection', function (socket) {
console.log("Co nguoi connect");
io.sockets.emit('serverguitinnhan', { noidung: "okbaby" });
socket.on('servernhantinnhan', function (data) {
// emit toi tat ca moi nguoi
io.sockets.emit('serverguitinnhan', { noidung: data });
// emit tới máy nguoi vừa gửi
socket.emit('serverguitinnhan', { noidung: data });
});
});
Không có nhận xét nào:
Đăng nhận xét