feat(vm): add vApp + VM terraform example
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
// Изменено: 2026-03-09
|
||||
// Изменено: 2026-04-10 (v0.1.3) — sendResult: если handler вернул строку начинающуюся с '<' → text/html.
|
||||
// HTTP-обёртка для serverless функций на Node.js 20.
|
||||
// Загружает модуль из SLESS_ENTRYPOINT или handler.js по умолчанию.
|
||||
// Формат SLESS_ENTRYPOINT: "module-name.functionName" (например: handler-http.handle)
|
||||
@@ -65,7 +66,7 @@ const server = http.createServer(async (req, res) => {
|
||||
|
||||
try {
|
||||
const result = await userHandle(event);
|
||||
sendJSON(res, 200, result);
|
||||
sendResult(res, 200, result);
|
||||
} catch (err) {
|
||||
console.error('Handler error:', err);
|
||||
sendJSON(res, 500, { error: err.message });
|
||||
@@ -73,6 +74,21 @@ const server = http.createServer(async (req, res) => {
|
||||
});
|
||||
});
|
||||
|
||||
function sendResult(res, status, data) {
|
||||
// Если handler вернул строку начинающуюся с '<' — HTML страница.
|
||||
// Если строка, но не HTML — text/plain. Иначе — JSON.
|
||||
if (typeof data === 'string') {
|
||||
const ctype = data.trimStart().startsWith('<')
|
||||
? 'text/html; charset=utf-8'
|
||||
: 'text/plain; charset=utf-8';
|
||||
const buf = Buffer.from(data, 'utf-8');
|
||||
res.writeHead(status, { 'Content-Type': ctype, 'Content-Length': buf.length });
|
||||
res.end(buf);
|
||||
return;
|
||||
}
|
||||
sendJSON(res, status, data);
|
||||
}
|
||||
|
||||
function sendJSON(res, status, data) {
|
||||
const body = JSON.stringify(data);
|
||||
res.writeHead(status, {
|
||||
|
||||
Reference in New Issue
Block a user