Problem serving local files with vweb (V-lang's default server)

211 views Asked by At

Description of the problem

According to V's doc, using vweb, in order to serve static files one just has to add the following line to a server:

app.mount_static_folder_at(os.resource_abs_path('dist'), '/content')

So, for instance: the following server is supposed to serve the content of the local directory ./dist at the address: http://localhost:8080/content

The complete code being :

import vweb 
import os

struct App {
    vweb.Context
}

fn main() {
    mut app := &App{}
    println(os.resource_abs_path('dist'))
    app.mount_static_folder_at(os.resource_abs_path('dist'), '/content')
    vweb.run(app, 8080)
}

["/"]
fn (mut app App) root() vweb.Result {
    return app.text('Hello from root')
}   


Expected Behavior

The server is supposed to serve the content of the local directory ./dist at the address: http://localhost:8080/content

Current Behavior

404 Not Found

Reproduction Steps

println(os.resource_abs_path('dist')) yields

C:\Users\serge\Documents\vue-tests\vue-tests-nomodules\v2\dist

Which is correct, plus:

ls lists

----                 -------------         ------ ----
d-----         2/11/2023   6:45 PM                dist
d-----         2/11/2023   6:45 PM                src
-a----         2/11/2023   5:53 PM            139 .editorconfig
-a----         2/11/2023   5:53 PM            148 .gitattributes
-a----         2/11/2023   5:53 PM            237 .gitignore
-a----         2/11/2023   6:06 PM             85 v.mod
-a----         2/12/2023  12:56 PM        2037248 v2.exe


PS C:\Users\serge\Documents\vue-tests\vue-tests-nomodules\v2> ls .\dist\


    Directory: C:\Users\serge\Documents\vue-tests\vue-tests-nomodules\v2\dist


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----         2/11/2023   6:32 PM            295 app.html
-a----         2/11/2023   6:42 PM            295 index.html

Possible Solution

No response

Additional Information/Context

Screenshot (257)

No response

V version

V 0.3.3 90591eb

Environment details (OS name and version, etc.)

Windows 11

1

There are 1 answers

0
V-Light On

A bit too late, but I had a similar issue lately.

Apparently, it does matter how you start the vweb server.

// <my_project>/src/main.v
const (
    assets_dir_name   = 'assets'
    assets_mount_path = os.join_path(os.resource_abs_path('.'), 'src', assets_dir_name) // assets_mount_path == ./src/assets
)

fn new_app() &App {
    mut app := &App{}
    // note: /assets/css and /assets/js as `mount_path` of mount_static_folder_at() must start with forward-slash (/)
    // all *.css in assets_mount_path/css -> /assets/css
    // all *.js in assets_mount_path/js -> /assets/js
    app.mount_static_folder_at(os.join_path(assets_mount_path, 'css'), '/assets/css')
    app.mount_static_folder_at(os.join_path(assets_mount_path, 'js'), '/assets/js')
    return app
}

fn main() {
    vweb.run_at(new_app(), vweb.RunParams{
        host: server_host
        port: server_http_port
        family: .ip
    }) or { panic(err) }
}

and in my temaplte

    <title>${page_title}</title>
    @css '/assets/css/main.css'
    @js '/assets/js/main.js'

You could also use path in href like

<script src="/assets/js/main.js" defer></script>

In order to run this properly just

v run . from your <my_project> and not from <project_dir>/src

enter image description here