Reputation: 1369
i am running the server in nodejs but when i try to access it through reactjs i am getting an error as TypeError: (0 , _serverRender2.default) is not a function. what should i do??
import config from './config';
import apiRouter from './api';
import express from 'express';
import path from 'path';
import sassMiddleware from 'node-sass-middleware';
import serverRender from './serverRender';
const server=express();
server.set('view engine','ejs');
server.use(sassMiddleware({
src:path.join(__dirname,'sass'),
dest:path.join(__dirname,'public')
}));
server.get('/',(req,res)=> {
serverRender()
.then(content => {
res.render('index', {
content
});
})
.catch(console.error);
});
server.use(express.static('public'));
server.use('/api', apiRouter);
server.listen(config.port, () => {
console.info('express listening on port ', config.port);
});
this is my serverRender file
import React from 'react';
import ReactDOMServer from 'react-dom/server';
import App from './src/components/App';
import axios from 'axios';
//import config from './config';
const serverRender = () =>{
axios.get('http://localhost:8080/api/contests')
.then(resp=>{
return ReactDOMServer.renderToString(<App initialContests = {resp.data.contests}/>);
// console.log(resp.data);
});
}
export default serverRender;
Upvotes: 1
Views: 647
Reputation: 837
You should check the code you wrote in serverRender.js
The error you see tells you that you don't export any modules from serverRender.js as default
Upvotes: 0
Reputation: 9032
serverRender()
doesn't return Promise
. You can try to the callback based approach.
const serverRender = (callback) =>{
axios.get('http://localhost:8080/api/contests')
.then(resp=>{
callback(ReactDOMServer.renderToString(<App initialContests = {resp.data.contests}/>));
});
}
serverRender.js
server.get('/',(req,res)=> {
serverRender(content => {
res.render('index', {
content
});
});
});
Upvotes: 1