Back End Development and APIs Projects - URL Shortener Microservice

Tell us what’s happening:

i dont understand mongodb is giving an error of permission denied for

Your code so far

require('dotenv').config();

const express = require('express');

const cors = require('cors');

const app = express();

const { MongoClient } = require('mongodb');

const client = new MongoClient(process.env.PORT)

const db = client.db("urlshortner")

const urls = db.collection("urls")

// Basic Configuration

const port = process.env.PORT || 3000

app.listen(port, () => {

console.log(`server running on port ${port}`)

});

app.use(cors());

app.use(express.json())

app.use(express.urlencoded({extended: true}))

app.use('/public', express.static(`${process.cwd()}/public`));

app.get('/', function(req, res) {

res.sendFile(process.cwd() + '/views/index.html');

});

// Your first API endpoint

app.post('/api/shorturl', function(req, res) {

console.log(req.body)

res.json({ greeting: 'hello API' });

});

app.listen(port, function() {

console.log(`Listening on port ${port}`);

});

and here is my error:

> shorturl@0.0.3 start
> node index.js

node:events:497
      throw er; // Unhandled 'error' event
      ^

Error: listen EACCES: permission denied mongodb+srv://moshe_david_27:@cluster0.b6hdqpq.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0
    at Server.setupListenHandle [as _listen2] (node:net:1881:21)
    at listenInCluster (node:net:1946:12)
    at Server.listen (node:net:2061:5)
    at Function.listen (/workspace/boilerplate-project-urlshortener/node_modules/express/lib/application.js:635:24)
    at Object.<anonymous> (/workspace/boilerplate-project-urlshortener/index.js:13:5)
    at Module._compile (node:internal/modules/cjs/loader:1358:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1416:10)
    at Module.load (node:internal/modules/cjs/loader:1208:32)
    at Module._load (node:internal/modules/cjs/loader:1024:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:174:12)
Emitted 'error' event on Server instance at:
    at emitErrorNT (node:net:1925:8)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
  code: 'EACCES',
  errno: -13,
  syscall: 'listen',
  address: 'mongodb+srv://moshe_david_27:@cluster0.b6hdqpq.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0',
  port: -1
}

please help me solve this problem…

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36

Challenge Information:

Back End Development and APIs Projects - URL Shortener Microservice

const client = new MongoClient(process.env.PORT)

const port = process.env.PORT || 3000

app.listen(port, () => {

console.log(`server running on port ${port}`)

});

process.env.PORT can’t be both a port number and the connection string.

Looking at the error I’m guessing it is the connection string. So, you are passing the MongoDB connection string as the first argument to app.listen instead of a port number. Also, you shouldn’t have two app.listen in the code. Remove the first and leave the last.

TL;DR: make sure you pass a port number to app.listen and only call it one time in the code.


Just to explain what I mean*

The Express docs on app.listen says:

NOTE: All the forms of Node’s http.Server.listen() method are in fact actually supported.

server. Listen()
Possible signatures:

server.listen(handle[, backlog][, callback])
server.listen(options[, callback])
server.listen(path[, backlog][, callback]) for IPC servers
server.listen([port[, host[, backlog]]][, callback]) for TCP servers

I didn’t test it but I would assume passing a URI as the first argument to app.listen may cause it to use the server.listen(path[, backlog][, callback]) version.