G
G
g00dv1n2016-10-28 14:31:06
JavaScript
g00dv1n, 2016-10-28 14:31:06

How to correctly set a static folder in koa?

There is a public folder. It's all static.
Usually connected:

app.use(require('koa-static')(__dirname + '/public'));

I create routes like this:
const router = require('koa-router')();

router.use('/', require('./index').routes());
router.use('/lockers', require('./lockers').routes());
router.use('/decryptors', require('./decryptors').routes());
router.use('/faq',require('./faq').routes());
router.use('/about', require('./about').routes());
router.use('/contacts', require('./contacts').routes());

module.exports = router;

In app.js then I connect it like this:
// mount root routes  
app.use(router.routes());

The problem is the following. There is such a route:
const router = require('koa-router')();
const config = require('../config');
const findLocker = require('../helpers/identifier');
const fileUpload = require('../helpers/fileUpload');

router.get('/', function *(next) {
    yield this.render('pages/lockers');
});

router.get('/:id', function *(next) {
    this.body = 'Single Locker Page: [Locker With Id - ' + this.params.id + ']';
});

router.post('/', function *(next) {
    // the body isn't multipart, so busboy can't parse it
    if (!this.request.is('multipart/*')) return yield next;

    let uploadPath = yield fileUpload(config.uploadDir, this, ['.html', '.txt']);
    this.body = yield findLocker(uploadPath);
});

module.exports = router;

When using the /lockers root route. Then all styles are taken along the path: localhost/stylesheet/blablabla.css
But when the /lockers/:id route is used, then it is taken along the path localhost/stylesheet/lockers/blablabla.css The
style is usually connected.
<link rel="stylesheet" href="stylesheets/style.css">

How to fix it?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Taratin, 2016-10-28
@g00dv1n

It is better not to distribute static through node at all, but to use, for example, nginx.
All the same, you will not have a bare node outside.
Everything is correct
For /lockers css will point to localhost/stylesheet/blablabla.css
For /lockers/<something else> to localhost/lockers/stylesheet/blablabla.css
since we went down a level, but we are looking for css relative to the current path .
Use absolute paths relative to the root.

A
Alexander, 2016-12-27
@kentuck1213

просто обычный слайдер, не сверх.
kenwheeler.github.io/slick

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question