This commit is contained in:
satori 2021-11-30 22:55:34 +08:00
parent 8926c0a316
commit 9cb2ec688e
2 changed files with 33 additions and 45 deletions

View File

@ -297,6 +297,38 @@ fetch('/like/SOAPSAdaw', {
#### 调频广播
```javascript
let socket = new WebSocket("ws://localhost:2333");
socket.onopen = function(e) {
alert("[open] Connection established");
alert("Sending to server");
socket.send("My name is John");
};
socket.onmessage = function(event) {
alert(`[message] Data received from server: ${event.data}`);
};
socket.onclose = function(event) {
if (event.wasClean) {
alert(`[close] Connection closed cleanly, code=${event.code} reason=${event.reason}`);
} else {
// e.g. server process killed or network down
// event.code is usually 1006 in this case
alert('[close] Connection died');
}
};
socket.onerror = function(error) {
alert(`[error] ${error.message}`);
};
```
@ -326,49 +358,5 @@ fetch('/like/SOAPSAdaw', {
查询示例:
```javascript
fetch('/book').then(Response => Response.json()).then(data => {
console.log(data)
})
// 返回结果
[
{_id:'xxx', data:'xxxx'},
{_id:'xxx', data:'xxxx'}
]
```
创建示例:
```javascript
fetc('/book', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
name: '创建一本新书',
data: '书的内容'
}),
}).then(Response => Response.json()).then(data => {
console.log(data)
})
// 返回结果
{
_id: 'xxx',
uid: 'xxx',
name: '创建一本新书',
data: '书的内容'
}
```
#### 对象实体

View File

@ -351,7 +351,7 @@ app.use(express.urlencoded({ extended: false }))
app.use(session({ secret: 'kana', name: 'sid', resave: false, saveUninitialized: false, cookie: { maxAge: 180 * 24 * 3600000 }, store: session_store }))
app.use('/data/file/', express.static('data/file'))
app.ws('/ws', websocketer)
app.ws('/', websocketer)
app.route('/').get((req, res) => res.send(`<DOCTYPE html><p> Hello World</p>`))
app.route('/user').post(object_create)
app.route('/account').get(online, profile)