question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Not serving my public folder.

See original GitHub issue

I have tried following codes but i am unable to see static files.

'use strict'

const path = require('path');
const router = require('./routes')
const Koa = require('koa')
const views = require('koa-views')
const passport = require('koa-passport')
const serve = require('koa-static')

const app = new Koa()

// Serving public assets.
app.use(serve('public'))
app.listen(8080);

My folder structure is that on root i have public folder.


public/
├── css
   └── app.css
web
├── index.js    // This contains above code. the code is complete. 

index.js  // Start script. node index.js

I have tried to provide all these paths in serve arguments

app.use(serve(__dirname+ '/../public'))  // resolves to valid path
app.use(serve('/<complete-absolute-path>/public')) // resolves to valid path.

app.use(serve(__dirname+ '/public')) // for testing only, path is incorrect.
app.use(serve('./public'))  // for testing only, path is incorrect.

Also when i added logs to check if path is resolved correctly then first two paths from above commands are valid.

but when i visit

http://localhost:8080/public/css/app.css

It won’t serve static file.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

26reactions
snollygollycommented, Oct 24, 2017

I think the issue here is that when you do app.use(serve('.')), you’re serving your whole directory, so if your files are in /public, you make a request to http://.../public/files to get them. When you use app.use(serve('./public')), that same file is now served at the web root: http://.../files. Another issue suggests using koa-mount to resolve this, so that’s what I did:

const serve = require("koa-static");
const mount = require("koa-mount");
app.use(mount("/public", serve("./public")));

Try that and see if it works for you?

6reactions
Yugloocamaicommented, Sep 13, 2018

app.use(mount("/public", serve("./public"))); does nothing for me.

Read more comments on GitHub >

github_iconTop Results From Across the Web

node.js - express.static not serving .html file from public folder
To serve your documents, just rename document.html to index.html and bind the directory: app.use('/document', express.static('document'));.
Read more >
Create and share a public folder in Outlook - Microsoft Support
Public folders in Outlook give you a great place to collect, organize, and share information about particular topics or projects within an organization....
Read more >
I'm having trouble with a shared folder - Dropbox Help
There are a few situations when your shared folders might not work as expected. Learn how to fix problems with shared folders.
Read more >
Serving static files in Express
For example, use the following code to serve images, CSS files, and JavaScript files in a directory named public : app.use(express.static('public')).
Read more >
Serving Static Resources in Node.js - TutorialsTeacher
The following example serves static resources from the public folder under the ... This will not break your application even if you run...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found