Skip to content

Commit 76bb570

Browse files
author
farfromrefug
committed
chore: test server for image with headers
1 parent 794ee5d commit 76bb570

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

demo-snippets/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
"name": "@nativescript-community/template-snippet",
33
"private": true,
44
"version": "0.0.1",
5+
"scripts": {
6+
"start-server":"node server_test/index.js"
7+
},
58
"dependencies": {
69
"@nativescript-community/ui-image": "*",
710
"@nativescript-community/ui-image-colorfilter": "*",

demo-snippets/server_test/index.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const http = require('http');
2+
const fs = require('fs');
3+
const path = require('path');
4+
5+
const server = http.createServer((req, res) => {
6+
if (req.url === '/image' && req.method === 'GET') {
7+
// Check the authorization header
8+
const authToken = req.headers['authorization'];
9+
if (!authToken || authToken !== 'Bearer 1234') {
10+
res.writeHead(401, { 'Content-Type': 'text/plain' });
11+
res.end('Unauthorized: No token provided or token is invalid');
12+
return;
13+
}
14+
15+
// Path to the image file
16+
const imagePath = path.join(__dirname, '..', 'assets', 'images', 'dessert.jpg');
17+
18+
// Read and send the image file
19+
fs.readFile(imagePath, (err, data) => {
20+
if (err) {
21+
console.log(err);
22+
res.writeHead(500, { 'Content-Type': 'text/plain' });
23+
res.end('Error reading image file');
24+
return;
25+
}
26+
res.writeHead(200, { 'Content-Type': 'image/jpeg' });
27+
res.end(data);
28+
});
29+
} else {
30+
// Handle other requests or methods
31+
res.writeHead(404, { 'Content-Type': 'text/plain' });
32+
res.end('Not Found');
33+
}
34+
});
35+
36+
const PORT = 3000;
37+
server.listen(PORT, () => {
38+
console.log(`Server running on port ${PORT}`);
39+
});

0 commit comments

Comments
 (0)