Sapper: Setup sapper with express

Created on 26 Jul 2019  路  3Comments  路  Source: sveltejs/sapper

I want to setup express instead of polk and have the setup below but get an error when I run npm run dev.

express() // You can also use Express
    .use(
        compression({ threshold: 0 }),
        sirv('static', { dev }),
        authenticationMiddleware(),
        sapper.middleware({
            session: (req, res) => ({
                user: req.user
            })
        })
    )
    .listen(PORT, err => {
        if (err) console.log('error', err);
    });
TypeError: Router.use() requires a middleware function but got a Promise
    at Function.use (/storage/utils/sapper/node_modules/express/lib/router/index.js:458:13)
    at Function.<anonymous> (/storage/utils/sapper/node_modules/express/lib/application.js:220:21)
    at Array.forEach (<anonymous>)
    at Function.use (/storage/utils/sapper/node_modules/express/lib/application.js:217:7)
    at eval (webpack:///./src/server.js?:28:6)
    at Module../src/server.js (/storage/utils/sapper/__sapper__/dev/server/server.js:302:1)
    at __webpack_require__ (/storage/utils/sapper/__sapper__/dev/server/server.js:21:30)
    at /storage/utils/sapper/__sapper__/dev/server/server.js:85:18
    at Object.<anonymous> (/storage/utils/sapper/__sapper__/dev/server/server.js:88:10)
    at Module._compile (internal/modules/cjs/loader.js:701:30)

All 3 comments

I can't reproduce this with the default Rollup or webpack Sapper templates by replacing polka@next with express@latest. Please ask support questions on Stack Overflow or in our Discord chatroom. Accompany bug reports with a reproduction of the issue.

You're probably importing it wrong.

import express from 'express';

express().use(...)

You actually need to use a common js require syntax like thos:
const express = require('express');

so your code should like this:

const express = require('express ')

const app = express();

app.use(compression({ threshold: 0 }));
app.use(sirv('static', { dev }));
app.use(sapper.middleware());

app.listen(PORT, (err) => {
if(err) console.log(err)
})

Was this page helpful?
0 / 5 - 0 ratings

Related issues

benmccann picture benmccann  路  3Comments

Snugug picture Snugug  路  4Comments

UnwrittenFun picture UnwrittenFun  路  4Comments

BMorearty picture BMorearty  路  3Comments

nolanlawson picture nolanlawson  路  4Comments