0

I want to serve another static page for the route "/about", now I have :

app.use(express.static(path.resolve(__dirname, '..', 'build')));

app.get('/', (req, res) => {
  res.sendFile(path.resolve(__dirname, '..', 'build', 'index.html'));
});
app.get('/about', function (req, res) {
    console.log("here")
  res.sendFile(path.resolve(__dirname, '..', 'build', 'about.html'));
});

and I also have a post request

app.post('/getfrontpage', (req, res) => {
    console.log("FIRST PAGE", req.body.params.page)
});

The post request works, I use axios to make a call, but the "/about" route doesn't work when I'm changing the url

Elena
  • 569
  • 3
  • 7
  • 19
  • Hi, Hard to tell. I see you have a react front end. Your react front end routes have to be exactly the same as express api's if not using hash routes. Another option may be to have an array of your api functions passed to app.use on '/' and use next() to call next function in the stack. - The truth is out there – henhen Sep 07 '19 at 03:28
  • You can also try to have another app.use on '/about' route to serve about static file specifally then use next() to call the callback to send file. Hope this helps. - The truth is out there – henhen Sep 07 '19 at 03:39

1 Answers1

0

Do you have any errors on the server side?

The example above works for me: https://runkit.com/dionnis/5ab936152f32d7001211d85f

Note: I changed app.post('/getfrontpage' to app.get('/getfrontpage'

Dennis Liger
  • 1,488
  • 2
  • 13
  • 28
  • I don't have any errs, but I can't see the console.log("here"), and I still see the index.html instead of about.html, I think that the problem with a webpack, created by "create react app" – Elena Mar 26 '18 at 18:27
  • 1
    Webpack for "create react app" and the express app is completely different things. Can you just test express app first by manually creating "about.html" file and opening it? – Dennis Liger Mar 26 '18 at 19:46
  • I have about.html file, but every time I'm trying to get "/about" I still see the index.html content – Elena Mar 26 '18 at 20:58
  • 1
    Try to change the order of `app.get('/',` and `app.get('/about'` https://stackoverflow.com/questions/32603818/order-of-router-precedence-in-express-js BTW: don't forget after any changes restart the server. – Dennis Liger Mar 26 '18 at 21:26
  • Changing the order doesn't work, but it works if I completely remove app.get('/') – Elena Mar 26 '18 at 21:45
  • I think I have a problem because of this line : app.use(express.static(path.resolve(__dirname, '..', 'build'))); but without it the app doesnt work at all – Elena Mar 26 '18 at 22:07
  • Actually, you are right. Having static middleware - you don't need to specify `about.html` page at all, it should work without `app.get('/about', ` – Dennis Liger Mar 27 '18 at 00:37