建立基本資料
npm init
npm install express
直接抓取package-lock.json中所記載的套件版本
並全數安裝
npm install
刪除
npm uninstall express
更新
npm update express
const routers = require('./routes');
const app = require('express');
var express = require('express');
var app = express();
var fs = require('fs');
var bodyParser = require('body-parser');
var port=process.env.PORT || 5000;
//get server
app.get("/"+"js_package.js",function (req,res) {
//get url 上的資料(問號後面的字)
var number=req.query.number
res.end("hello world")
})
var server = app.listen(port, function () {
var host = server.address().address
var port = server.address().port
console.log("應運實力,訪問地址為 http://localhost:%s", port)
//console.log("应用实例,访问地址为 http://%s:%s",host, port)
})
var express = require('express');
var app = express();
var fs = require('fs');
var bodyParser = require('body-parser');
var port=process.env.PORT || 5000;
//
app.get("/"+"js_package.js",function (req,res) {
res.sendFile(__dirname+"/js_package.js")
})
var server = app.listen(port, function () {
var host = server.address().address
var port = server.address().port
console.log("应用实例,访问地址为 http://localhost:%s", port)
//console.log("应用实例,访问地址为 http://%s:%s",host, port)
})
node
app.get('/main', function(req, res) {
var name = 'hello';
res.render(__dirname + "/views/layouts/main.html", {name:name});
});
<h1><%= name %></h1>
var express = require('express');
var app = express();
var fs = require('fs');
var bodyParser = require('body-parser');
var port=process.env.PORT || 5000;
//接收檔案大小限制為 50MB
app.use(bodyParser.json({limit: '50mb'}));
app.use(bodyParser.urlencoded({limit: '50mb', extended: true}));
//post server
app.post('/add_data', function (req, res) {
//post data
var add_data=req.body
data=JSON.parse(data)
res.end("收到"+JSON.stringify(data))
});
})
var fs = require('fs');
var datafile="./data.json"
var data=fs.readFileSync(datafile)
var fs = require('fs');
var datafile="./data.json"
fs.readFile(datafile, 'utf8', function (err, data) {
console.log(data)
})
var fs = require('fs');
var datafile="./data.json"
var write_data={
"data":"change"
}
fs.writeFile(datafile,JSON.stringify(write_data, null, "\t"), function (err) {
console.error(err)
})
server and client 雙向的即時溝通
注意:目前只在筆電測試 似乎只有在用同一個路由器下 訪問ip位置有效
node.js
var http = require("http");
var url = require('url');
var fs = require('fs');
var io = require('socket.io'); // 加入 Socket.IO
var ip = require("ip");//用來取得區域ip
var server = http.createServer(function(request, response) {
console.log('Connection');
var path = url.parse(request.url).pathname;
switch (path) {
case '/':
response.writeHead(200, {'Content-Type': 'text/html'});
response.write('Hello, World.');
response.end();
break;
case "/socket.html" :
res.sendFile(__dirname+"/socket.html")
break;
}
}
//取得區域ip
var hostnme=ip.address()
server.listen(8000,hostnme,()=> console.log("work"));
var server_io = io.listen(server);
server_io.sockets.on('connection', function(socket) {
setInterval(function() {
var send_data={
'time': new Date(),
}
socket.emit('server_data', send_data);
//每1秒寄送一次時間
},1000)
//接收client寄來的data
socket.on('client_data', function(data) {
console.log("client_sent_data"+data.tostring())
});
}
html
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.3.1/socket.io.js"></script>
</head>
javascript
var socket = io.connect();
//接收來自 server 的資料
socket.on('server_data', function(data){
var sent_data={
'letter': "ok"
}
//回傳訊息給 server
socket.emit('client_data',sent_data );
var delay_time= parseInt(new Date() - Date.parse(data.time))
//顯示延遲時間
document.body.innerHTML="delay"+delay_time+"/ms"
}
npm install -g pkg
File Tree
socket_project
├── node_modules
├── socket.io.js
├── package.json
├── package-lock.json
└── dist
└── socket.html
json
//指定執行黨
"bin":"socket.io.js",
"pkg": {
//靜態檔案存放位置
"assets": [
"dist/**/*"
]
}
node.js
socket.io 範例
var http = require("http");
var url = require('url');
var fs = require('fs');
var io = require('socket.io'); // 加入 Socket.IO
var ip = require("ip");
port=8000
//dir 綠色
console.dir ( ip.address() +":"+port+"/socket.html");
//ip localtion by pconfig
var hostnme=ip.address()
var server = http.createServer(function(request, response) {
console.log('Connection');
var path = url.parse(request.url).pathname;
switch (path) {
case '/':
fs.readFile(__dirname+"/dist/socket.html", function(error, data) {
response.writeHead(200, {"Content-Type": "text/html"});
response.write(data, "utf8");
response.end()
})
break;
case '/socket.html':
fs.readFile(__dirname+ "/dist/"+ path, function(error, data) {
if (error){
response.writeHead(404);
response.write("opps this doesn't exist - 404");
} else {
response.writeHead(200, {"Content-Type": "text/html"});
response.write(data, "utf8");
}
response.end();
});
break;
case '/test':
response.writeHead(200, {'Content-Type': 'text/html'});
response.write('Hello, World.');
response.end();
break;
default:
response.writeHead(404);
response.write("opps this doesn't exist - 404");
response.end();
break;
}
});
server.listen(port,hostnme,()=> console.log("work"));
var serv_io = io.listen(server);
serv_io.sockets.on('connection', function(socket) {
//發送訊息
var dotime=0
setInterval(function() {
var send_data={
'time': new Date(),
"image":"image_64",
}
socket.emit('server_data', send_data);
//dotime+=1;
dotime++;
},100);
//30fps=33ms
socket.on('client_data', function(data) {
//image_64=data.image
});
//接收
});
pkg -t win package.json
pkg package.json
View the slide with "Slide Mode".
Jun 5, 2025The old render pipeline used to directly transport the rendered pixel results to the screen, without providing a way to save the rendered image.
Nov 24, 2024View the slide with "Slide Mode".
Aug 28, 2023只會新增或修改已存在檔案不會刪除
Jun 26, 2023or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up