Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

connect ECONNREFUSED 127.0.0.1:80 #3960

Closed
mbj36 opened this issue Mar 7, 2018 · 7 comments
Closed

connect ECONNREFUSED 127.0.0.1:80 #3960

mbj36 opened this issue Mar 7, 2018 · 7 comments

Comments

@mbj36
Copy link

mbj36 commented Mar 7, 2018

Hi Guys, Thanks for making so awesome NExT.js

I am facing this issue connect ECONNREFUSED 127.0.0.1:80

My server.js file looks like this -

import http from 'http'
import express from 'express'
import morgan from 'morgan'
import bodyParser from 'body-parser'
import connect from './db'
import schedule from 'node-schedule'
import apiRoutes from './api/routes/index'
import next from 'next'
import dotenv from 'dotenv'

import { saveMeetupData } from './api/scripts/meetupData'

dotenv.config()

const port = process.env.PORT
const env = process.env.NODE_ENV
const dev = env !== 'production'
const app = next({ dev })
const handle = app.getRequestHandler()

connect() //MongoDB connect

app
  .prepare()
  .then(() => {
    const server = express()
    server.use(morgan('dev')) //Logger

    server.use(
      bodyParser.urlencoded({
        extended: true
      })
    )

    schedule.scheduleJob('*/5 * * * *', function() {
      saveMeetupData('https://api.meetup.com/ReactJS-Bangalore/events')
      saveMeetupData(
        'https://api.meetup.com/ReactJS-Bangalore/events?status=past'
      )
      console.log('Ran the scheduler')
    })

    server.use(bodyParser.json())

    server.use((req, res, next) => {
      res.set('Access-Control-Allow-Origin', '*')
      res.set('Access-Control-Allow-Methods', 'POST,GET,OPTIONS,PUT,DELETE')
      res.set('Access-Control-Allow-Headers', 'Content-Type,Accept')
      res.set('Access-Control-Expose-Headers', 'X-Total-Count')
      res.set('X-Total-Count', '10')
      next()
    })

    server.use('/api', apiRoutes)

    server.get('/jobs/:id', (req, res) => {
      return app.render(req, res, '/jobdetail', req.params)
    })

    server.get('/resources/:item', (req, res) => {
      return app.render(req, res, '/resourcedetail', req.params)
    })

    server.get('*', (req, res) => {
      return handle(req, res)
    })
    server.listen(port, err => {
      if (err) throw err
      console.log(`> Ready on http://localhost:${port}`)
    })
  })
  .catch(err => {
    console.log('An error occurred, unable to start the server')
    console.log(err)
  })

export default app

Any help would be highly appreciated

@timneutkens
Copy link
Member

Not sure in which context you're getting the error. But if you're doing PORT=80 node server.js you'll need sudo rights, since you can't bind to 80 without them.

@mbj36
Copy link
Author

mbj36 commented Mar 7, 2018

@timneutkens My script looks like this

  "scripts": {
    "server": "nodemon -w server --exec \"babel-node server --presets es2015,stage-0\"",
    "build": "next build",
    "start": "NODE_ENV=production nodemon -w server --exec \"babel-node server --presets es2015,stage-0\"",
    "deploy": "now --public"
  }

@timneutkens
Copy link
Member

port is not defined, so it will never bind to any port. Try adding a default:
const port = process.env.PORT || 3000

@mbj36
Copy link
Author

mbj36 commented Mar 7, 2018

I tried but still getting the same error @timneutkens

@timneutkens
Copy link
Member

Still not sure where you get the error 🤔please provide a screenshot.

@mbj36
Copy link
Author

mbj36 commented Mar 7, 2018

screen shot 2018-03-07 at 1 52 10 pm

@ernestofreyreg
Copy link

I saw this too. But in my case was a silly mistake. So basically I was calling axios.get on a componentWillMount method. But only using a /api/.... part. Of course, this in the server context will resolve to a request to the default host:port 127.0.0.1:80. Changing the method from componentWillMount to componentDidMount solved the problem. Hope it helps.

@lock lock bot locked as resolved and limited conversation to collaborators May 16, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants