How to serve static files on CloudFoundry?

I have a svelte project that basically is a database of links to internal or external files represented visually as a table. Nothing uber-fancy.

So, I’ve read in other post on StackOverflow that I have to implement a route in order to properly serve static files. I ended up with the following code that perfectly works locally: the execution reaches the code, correctly defines the path of the file and serves the file to browser

the code is put into /src/routes/[...path/+server.js] file:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import path from 'node:path'
import fs from 'node:fs/promises'
import { error } from '@sveltejs/kit'
import { defineConfig } from 'vite';
export const GET = async ({ params }) => {
console.log("i've got the params ", params);
const workingFolder = import.meta.env.WORKING_DIR;
console.log("here is my url ", import.meta.url)
const pathName = `${workingFolder}/static${params.path}`;
console.log("here is the full path: ", pathName);
try {
const file = await fs.readFile(pathName);
return new Response(file, {
headers: {
'Content-Type': getMimeType(pathName),
},
});
} catch {
throw error(404, 'File not found.');
}
}
function getMimeType(filePath) {
const ext = path.extname(filePath).toLowerCase();
const mimeTypes = {
'.html': 'text/html',
'.css': 'text/css',
'.js': 'application/javascript',
'.json': 'application/json',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.gif': 'image/gif',
'.svg': 'image/svg+xml',
'.txt': 'text/plain',
};
return mimeTypes[ext] || 'application/octet-stream';
}
</code>
<code>import path from 'node:path' import fs from 'node:fs/promises' import { error } from '@sveltejs/kit' import { defineConfig } from 'vite'; export const GET = async ({ params }) => { console.log("i've got the params ", params); const workingFolder = import.meta.env.WORKING_DIR; console.log("here is my url ", import.meta.url) const pathName = `${workingFolder}/static${params.path}`; console.log("here is the full path: ", pathName); try { const file = await fs.readFile(pathName); return new Response(file, { headers: { 'Content-Type': getMimeType(pathName), }, }); } catch { throw error(404, 'File not found.'); } } function getMimeType(filePath) { const ext = path.extname(filePath).toLowerCase(); const mimeTypes = { '.html': 'text/html', '.css': 'text/css', '.js': 'application/javascript', '.json': 'application/json', '.png': 'image/png', '.jpg': 'image/jpeg', '.gif': 'image/gif', '.svg': 'image/svg+xml', '.txt': 'text/plain', }; return mimeTypes[ext] || 'application/octet-stream'; } </code>
import path from 'node:path'
import fs from 'node:fs/promises'
import { error } from '@sveltejs/kit'
import { defineConfig } from 'vite';

export const GET = async ({ params }) => {

    console.log("i've got the params ", params);

    const workingFolder = import.meta.env.WORKING_DIR;

    console.log("here is my url ", import.meta.url)

    const pathName = `${workingFolder}/static${params.path}`;

    console.log("here is the full path: ", pathName);

    try {
        const file = await fs.readFile(pathName);
        return new Response(file, {
            headers: {
                'Content-Type': getMimeType(pathName),
            },
        });
    } catch {
        throw error(404, 'File not found.');
    }
}

function getMimeType(filePath) {
    const ext = path.extname(filePath).toLowerCase();
    const mimeTypes = {
        '.html': 'text/html',
        '.css': 'text/css',
        '.js': 'application/javascript',
        '.json': 'application/json',
        '.png': 'image/png',
        '.jpg': 'image/jpeg',
        '.gif': 'image/gif',
        '.svg': 'image/svg+xml',
        '.txt': 'text/plain',
    };
    return mimeTypes[ext] || 'application/octet-stream';
}

the log output is usually something like it:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>i've got the params { path: '/uploads_folder/cls/20241216-01/9Mb-report.html' }
here is my url file:///C:/projects/project_name/src/routes/[...path]/+server.js
here is the full path: C:projectsproject_name/static/uploads_folder/cls/20241216-01/9Mb-report.html
</code>
<code>i've got the params { path: '/uploads_folder/cls/20241216-01/9Mb-report.html' } here is my url file:///C:/projects/project_name/src/routes/[...path]/+server.js here is the full path: C:projectsproject_name/static/uploads_folder/cls/20241216-01/9Mb-report.html </code>
i've got the params  { path: '/uploads_folder/cls/20241216-01/9Mb-report.html' }
here is my url  file:///C:/projects/project_name/src/routes/[...path]/+server.js
here is the full path: C:projectsproject_name/static/uploads_folder/cls/20241216-01/9Mb-report.html

Hovewer when I deploy that code to CloudFoundry VM, my code encounters ‘404 / not found’ error and it seems like it never reaches this branch of code, as no log messages available in the logs.

my vite.config.ts is:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>export default defineConfig({
plugins: [sveltekit()],
test: {
include: ['**/*.{test,spec}.{js,ts}']
},
define: {
'import.meta.env.VERSION': JSON.stringify(version),
'import.meta.env.WORKING_DIR': JSON.stringify(process.cwd()),
},
server: {
fs: {
strict: false,
},
}
});
</code>
<code>export default defineConfig({ plugins: [sveltekit()], test: { include: ['**/*.{test,spec}.{js,ts}'] }, define: { 'import.meta.env.VERSION': JSON.stringify(version), 'import.meta.env.WORKING_DIR': JSON.stringify(process.cwd()), }, server: { fs: { strict: false, }, } }); </code>
export default defineConfig({
    plugins: [sveltekit()],
    test: {
        include: ['**/*.{test,spec}.{js,ts}']
    },
    define: {
        'import.meta.env.VERSION': JSON.stringify(version),
        'import.meta.env.WORKING_DIR': JSON.stringify(process.cwd()),
    },
    server: {
        fs: {
            strict: false,
        },
    }
});

My deployment manifest.yml is also nothing fancy:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>---
applications:
- name: mywebsite
memory: 1G
instances: 1
routes:
- route: mywebsite.mycloudfoundry.com
stack: cflinuxfs4
buildpacks:
- https://github.com/cloudfoundry/nodejs-buildpack
services:
- serviceOne
- database
env:
PUBLIC_ENVOY_URL: "https://myenvoy.mycloudfoundry.com/"
BODY_SIZE_LIMIT: 25M
</code>
<code>--- applications: - name: mywebsite memory: 1G instances: 1 routes: - route: mywebsite.mycloudfoundry.com stack: cflinuxfs4 buildpacks: - https://github.com/cloudfoundry/nodejs-buildpack services: - serviceOne - database env: PUBLIC_ENVOY_URL: "https://myenvoy.mycloudfoundry.com/" BODY_SIZE_LIMIT: 25M </code>
---
applications:
  - name: mywebsite
    memory: 1G
    instances: 1
    routes:
      - route: mywebsite.mycloudfoundry.com
    stack: cflinuxfs4
    buildpacks:
      - https://github.com/cloudfoundry/nodejs-buildpack
    services:
      - serviceOne
      - database
    env:
      PUBLIC_ENVOY_URL: "https://myenvoy.mycloudfoundry.com/"
      BODY_SIZE_LIMIT: 25M

What blocks the route (or alters that) so the code is never reached and how to correct it?

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật