Add support for public static files. Closes #2234

This commit is contained in:
Gabe Kangas 2022-10-27 22:30:39 -07:00
parent d1051a895f
commit ae7c02b421
No known key found for this signature in database
GPG Key ID: 9A56337728BC81EA
2 changed files with 34 additions and 0 deletions

View File

@ -373,6 +373,9 @@ func Start() error {
chat.HandleClientConnection(w, r)
})
// Optional public static files
http.Handle("/public/", http.StripPrefix("/public/", http.FileServer(http.Dir("./public"))))
port := config.WebServerPort
ip := config.WebServerIP

View File

@ -0,0 +1,31 @@
var request = require('supertest');
request = request('http://127.0.0.1:8080');
const fs = require('fs');
const path = require('path');
function randomString(length) {
return Math.random().toString(36).substring(length);
}
const filename = `${randomString(20)}.txt`;
test('public static file should not exist', async (done) => {
await request.get(`/public/${filename}`).expect(404);
done();
});
test('public static file should exist', async (done) => {
// make public static files directory
try {
fs.mkdirSync(path.join(__dirname, '../../../public/'));
fs.writeFileSync(
path.join(__dirname, `../../../public/${filename}`),
'hello world'
);
} catch (e) {}
await request.get(`/public/${filename}`).expect(200);
done();
});