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.

is it possible to call the default route from another route in express

See original GitHub issue

Issue Description

Hi,

In webpack-hot-server-middleware you have your serverRender function and you set up dev and prod like this:

const express = require('express');
const path = require('path');
const app = express();

if (process.env.NODE_ENV !== 'production') {
    const webpack = require('webpack');
    const webpackDevMiddleware = require('webpack-dev-middleware');
    const webpackHotMiddleware = require('webpack-hot-middleware');
    const webpackHotServerMiddleware = require('webpack-hot-server-middleware');
    const config = require('./webpack.config.js');
    const compiler = webpack(config);
    app.use(webpackDevMiddleware(compiler, {
      serverSideRender: true
    }));
    app.use(webpackHotMiddleware(compiler.compilers.find(compiler => compiler.name === 'client')));
    app.use(webpackHotServerMiddleware(compiler));
} else {
    const CLIENT_ASSETS_DIR = path.join(__dirname, '../build/client');
    const CLIENT_STATS_PATH = path.join(CLIENT_ASSETS_DIR, 'stats.json');
    const SERVER_RENDERER_PATH = path.join(__dirname, '../build/server.js');
    const serverRenderer = require(SERVER_RENDERER_PATH);
    const stats = require(CLIENT_STATS_PATH);
    app.use(express.static(CLIENT_ASSETS_DIR));
    app.use(serverRenderer(stats));
}

Say I have a non root url like some-root is it possible for me to call the serverRenderer function without redirecting? All I can think to do is this.

express.Router().post('/other-route', (_: Request, res: Response) => {
  // do stuff

  res.redirect('/');
});

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
richardscarrottcommented, Aug 15, 2018

@dagda1 you can mount the middleware on any path you like, e.g.

const router = express.Router();
const serverRenderer = webpackHotServerMiddleware(compiler);
router.post('/other-route', serverRenderer);
router.get(serverRenderer);

Alternatively, you could even export a router from the server entry, e.g.

// server.js
const router = express.Router();
const render = (req, res) => res.send(renderToString(<div>Hello World</div>));
router.post('/other-route', render);
router.get(render);
export default router;
0reactions
dagda1commented, Aug 15, 2018

@richardscarrott thank you so much for taking the time to answer my question.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Setting a Default route in Express.js - GeeksforGeeks
Steps to set the default route in the express app: ... Step 1: Create your project folder. ... Step 4: Require 'express' and...
Read more >
javascript - Default route in Express.js - Stack Overflow
I want to setup a default route so that no matter what I type in the URL after localhost:port/ it should all get...
Read more >
Routing - Express.js
You can create chainable route handlers for a route path by using app.route() . Because the path is specified at a single location,...
Read more >
Express Tutorial Part 4: Routes and controllers - MDN Web Docs
The "about" route (reproduced below) is defined using the Router.get() method, which responds only to HTTP GET requests. The first argument to ...
Read more >
How to set up routing in an Express.js project using TypeScript
A route is a system that associates an HTTP request and a function that will execute a process. There can be some parameters...
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