• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
    • Questions
    • Teachers
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

174
Views
How to use cors in node
'use strict';

const app          = require('koa')();
const jwt          = require('koa-jwt');
const bodyParser   = require('koa-bodyparser');
const send         = require('koa-send');
const serve        = require('koa-static');
const jsonwebtoken = require('jsonwebtoken');
const error        = require('./middleware/error-middleware.js');
const router       = require('koa-router')();

// do not use this secret for production
const secret = 'supersecret';

app.use(error());
app.use(bodyParser());
app.use(serve('dist'));
app.use(jwt({ secret }).unless({
    path: [/^(?!\/api\/)/]
}));

// for example use, we just check for specific username/password
// in the real world, we would check a database and check hashed password
router.post('/auth', function* () {
    const username = this.request.body.username;
    const password = this.request.body.password;

    if (username !== 'chuck' && password !== 'norris') {
        this.throw(401, 'Invalid username/password.');
    }

    const user = {
        username,
        email: 'chuck@norris.com',
        firstName: 'Chuck',
        lastName: 'Norris',
        roles: ['admin']
    };

    const token = jsonwebtoken.sign(user, secret, {
        issuer: 'localhost',
        audience: 'someaudience',
        expiresIn: '1d'
    });

    this.body = { token };
});

app.use(router.routes());

app.use(function* () {
    yield send(this, '/dist/index.html');
});

app.on('error', err => console.log(err));

app.listen(3000, () => console.log('example running on port 3000'));

How to solve this error? If I run a server with the port 80, and I try to use XMLHttpRequest I am getting this error: Error: listen EADDRINUSE Why is it problem for NodeJS, if I want to do a request, while I run a server on the port 80? For the webbrowsers it is not a problem: I can surf on the internet.

almost 3 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Your problem has nothing to do with CORS.

You are trying to create a server that listens for incoming requests on port 80. (The code you’ve provided says port 3000, but I’ll assume you are asking about what happens when you change that).

The error message says you can’t run a server that is listening on that port, because it is in use. This means that you already have a server (possibly another instance of the same server, but more likely a different one such as Apache HTTP) already running and using port 80.

You can’t have two servers listening on the same port. If a browser make an HTTP request to port 80, then the computer wouldn’t know which of the two servers the request was intended for.

It is not a problem for web browsers because they do not listen for incoming requests. They make outgoing requests.

almost 3 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error