When I upload details from a form to an end-point it works in dev server I can console.log and see the details.
But in production, both now and heroku, it returns error 501 not implemented.
It won't even log the first statement (content received) when I post to /api/contact-us/add
Using server.get instead of post doesn't help either
const express = require('express');
const next = require('next');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const sgMail = require('@sendgrid/mail');
const dev = process.env.NODE_ENV !== 'production';
const app = next({ dev });
const handle = app.getRequestHandler();
const Contact = require('./database/ContactModel');
app.prepare()
.then(() => {
const server = express();
server.use(bodyParser.urlencoded({ extended: true }));
server.use(bodyParser.json());
server.post('/api/contact-us/add', (req, res) => {
console.log('Content received');
console.log(req.body);
});
server.get('*', (req, res) => {
return handle(req, res);
});
server.listen(3000, '0.0.0.0', (err) => {
if (err) throw err;
console.log('> Ready on http://localhost:3000');
});
})
.catch((ex) => {
console.error(ex.stack);
process.exit(1);
});
| Tech | Version |
|---------|---------|
| next |3.0.6 |
| node |8.3.0 |
| OS |macOS |
| browser | |
| etc | |
I think I have a similar issue. Also with post, and for me with an http-proxy for graphql:
server.post('/graphql', (req, res) => {
proxy.web(req, res, { target: graphqlEndpoint })
})
PS
It used to work fine. 馃槙
I am experiencing the same issue, did either of you guys get to a solution for this?
A little more reading on the next.js website made me realise why my routes were not working.
If your using a custom server setup, be sure to amend your startup script accordingly: https://github.com/zeit/next.js#custom-server-and-routing
@designspin How does this fix the issue? I haven't had any luck
@mikeislearning what he is saying is that by default you'll have something like this in your package.json:
"start": "next start"
This is the state of the file if you followed the basic getting started guide. If you were to deploy this, say to a Heroku instance it will run fine however if you added additional routes you want to instruct the server to run the node serve.js script not next start. The latter won't include your custom routes, the former will. This accounts for why you might see different locally when running npm run dev and on a server which runs npm run start.
I'm having the same issue but nothing on this page seem to work so far.
This is my server.js
const express = require('express')
const next = require('next')
const bodyParser = require('body-parser')
const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev })
const handle = app.getRequestHandler()
const port = process.env.$PORT || 3000
const mailer = require('./lib/mailer')
app.prepare().then(() => {
const server = express()
server.use(bodyParser.json())
server.get('*', (req, res) => {
return handle(req, res)
})
server.post('/api', (req, res) => {
const { email = '', name = '', message = '', to } = req.body
mailer({ email, name, text: message, to }).then(() => {
res.send('success')
}).catch((error) => {
console.log('failed', error)
res.send('badddd')
})
})
server.listen(port, (err) => {
if (err) throw err
console.log(> Read on http://localhost:${port})
})
})
Here is my script from root level package.json
"scripts": {
"dev": "node server.js",
"start": "next start -p $PORT",
"heroku-postbuild": "next build"
},
Even When I change the start scrip to "NODE_ENV=production node server.js" it breaks.
help please
@josuesossou See my comment above, you have "start": "next start -p $PORT", in your package.json which you should change to NODE_ENV=production node server.js.
The root of the issue is that on Heroku the process is running start which calls next start -p $PORT to which your server.js is not included. If you change that command to call node server.js you'll find things work on that server.
@mwcbrent but I got a random error when I change it to NODE_ENV=p...
I get this error when I try npm run dev
vents.js:165
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE :::3000
at Server.setupListenHandle [as _listen2] (net.js:1345:14)
at listenInCluster (net.js:1386:12)
at Server.listen (net.js:1474:7)
at Function.listen (/Users/josue/Documents/Web Apps/PlayUP Landing Page/node_modules/express/lib/application.js:618:2
4)
at app.prepare.then (/Users/josue/Documents/Web Apps/PlayUP Landing Page/server.js:33:10)
at
at process._tickCallback (internal/process/next_tick.js:118:7)
Emitted 'error' event at:
at emitErrorNT (net.js:1365:8)
at process._tickCallback (internal/process/next_tick.js:114:19)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] dev: node server.js
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] dev script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/josue/.npm/_logs/2019-02-16T22_49_06_461Z-debug.log
Nvm, once I removed the $ in front of PORT, it's working in Heroku, but on my localhost, I have to change NODE_ENV=production node server.js to next start for it to work
Thank you @mwcbrent
Most helpful comment
@designspin How does this fix the issue? I haven't had any luck