andrejewski / smithsonian

web interface for Metalsmith
ISC License
36 stars 1 forks source link

Express routing #7

Closed brenwell closed 6 years ago

brenwell commented 6 years ago

Hey there, really nice tool you have made here, so thanks!

I am having some trouble getting it to work with express, maybe you can send me in the right direction.

Here is my setup file

const express = require('express')
const Smithsonian = require('smithsonian')
const Metalsmith  = require('metalsmith');
const Markdown    = require('metalsmith-markdownit');
const section     = require('metalsmith-section');
const layouts     = require('metalsmith-layouts');
const permalinks  = require('metalsmith-permalinks');
const watch       = require('metalsmith-watch');
const assets      = require('metalsmith-assets');
const collections = require('metalsmith-collections');
const debug       = require('metalsmith-debug');
const excerpts    = require('metalsmith-excerpts');

const server = Smithsonian(__dirname);

server.metalsmith
    .metadata()
    .source('./src')
    .destination('./build')
    .clean(true)

server
    .use(watch({
        paths: {
            "./src/**/*": true,
            "./layouts/**/*": "**/*.md",
            "./assets/**/*": "**/*",
        },
        livereload: true,
    }))
    .use(markdown)
    .use(excerpts())
    .use(section())
    .use(permalinks({
        pattern: ':path',
      // each linkset defines a match, and any other desired option
        linksets: [{
            match: { collection: 'posts' },
            pattern: 'posts/:path',
        },{
            match: { collection: 'pages' },
            pattern: ':path'
        }]
    }))
    .use(collections({
        pages: {
            pattern: 'pages/*.md',
            sortBy: 'date',
            reverse: true
        },
        posts: {
            pattern: 'posts/*.md',
            sortBy: 'date',
            reverse: true
        }
    }))
    .use(layouts({
        engine: 'pug',
        pretty: true,
    }))
    .use(assets({
        source: './assets', // relative to the working directory
        destination: './assets' // relative to the build directory
    }))
    .use(debug())
    .build(function(err, files) {
        if (err) { throw err; }
    })

express()
  .use('/smithsonian', server.router())
  .listen(3000);

And if I access this url http://localhost:3000/smithsonian I get Cannot GET /smithsonian

Its probably something super simple, so sorry in advance

andrejewski commented 6 years ago

Hey @brenwell, just got some time to look at this. Something unique about Smithsonian is that the user-interface is decoupled from the data API. Using server.router() only adds the data API, not any user interface. Try:

express()
  .use('/api', server.router())
  .use('/smithsonian', require('smithsonian/standard')('/api'))
  .listen(3000);

This will both the data API and the default user interface.

This is documented, poorly.

brenwell commented 6 years ago

I see, I will give it a go, thanks

brenwell commented 6 years ago

worked, thanks

andrejewski commented 6 years ago

Great!