File size: 1,088 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import { join } from 'path';
import express from 'express';
import React from 'react';
import { renderToString } from 'react-dom/server';
import { findResultsState } from 'react-instantsearch-dom/server';
import { createApp } from './createApp';
import template from './template';

const server = express();

server.use('/assets', express.static(join(__dirname, 'assets')));

server.get('/', async (_, res) => {
  const { App, props } = createApp();

  const searchState = {
    query: 'iPhone',
    page: 5,
    refinementList: {
      brand: ['Apple'],
    },
  };

  const resultsState = await findResultsState(App, {
    ...props,
    searchState,
  });

  const initialState = {
    searchState,
    resultsState,
  };

  const plainHTML = renderToString(<App {...props} {...initialState} />);

  res.send(
    template({
      body: plainHTML,
      title: 'Hello World from the server',
      initialState: JSON.stringify(initialState),
    })
  );
});

server.listen(8080, () => {
  // eslint-disable-next-line no-console
  console.log('Listening on: http://localhost:8080');
});