1

I'm starting my jouney in IT world building an API from a Custom Url Shortner Service and using mongodb Atlas.

I've been follwing up this tutorial to understand a little more but everytime i send a request i receive this error:

'POST http://localhost:5000/api/url/shorten

org.apache.http.conn.HttpHostConnectException: Connect to localhost:5000 [localhost/127.0.0.1] failed: Connection refused: connect'

here the way it is:

index.js

const express = require('express');
const connectDB = require('./config/db');

const app = express();

// Connect to database
connectDB();

app.use(express.json({ extented: false}));

// Define Routes
app.use('/', require('./routes/index'));
app.use('/api/url', require('./routes/url'));

const PORT = 5000;

app.listen(PORT, () => console.log(`Server running on port ${PORT}`));

`

package.json

{
  "name": "url_shortner_service",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "node index",
    "dev": "nodemon index"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "config": "^3.3.9",
    "express": "^4.18.2",
    "mongoose": "^7.0.1",
    "shortid": "^2.2.16",
    "valid-url": "^1.0.9"
  },
  "devDependencies": {
    "nodemon": "^2.0.21"
  }
} 
`

routs\

url.js

{
  "name": "url_shortner_service",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "node index",
    "dev": "nodemon index"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "config": "^3.3.9",
    "express": "^4.18.2",
    "mongoose": "^7.0.1",
    "shortid": "^2.2.16",
    "valid-url": "^1.0.9"
  },
  "devDependencies": {
    "nodemon": "^2.0.21"
  }
}
`

index.js

`    const express = require('express');
    const router = express.Router();

    module.exports = router;

`

requests\

api.http

POST http://localhost:5000/api/url/shorten
Content-Type: application/json
  {
  "longUrl": "https://stackoverflow.com/questions/48419801/org-apache-http-conn-httphostconnectexception-connect-to-localhost19538-local"
  }

###

models\

  const mongoose = require('mongoose');

    const urlSchema = new mongoose.Schema({
        urlCode: String,
        longUrl: String,
        shortUrl: String,
        date: { type: String, default: Date.now}
    });

    module.exports = mongoose.model('Url', urlSchema);

config
default.json

{
   "mongoURI": "mongodb+srv://apishort:<apishort>@cluster0.30vcjuk.mongodb.net/?retryWrites=true&w=majority",
    "baseUrl": "http://localhost:5000"
}`

db.js

const mongoose = require('mongoose');
const config = require('config');
const db = config.get('mongoURI');

const connectDB = async () => {
    try {
        await mongoose.connect(db,
            {
                useNewUrlParser: true
            });

        console.log('MongoDB Connected...');
    } catch (err) {
        console.error(err.message);
        process.exit(1);
    }
};

module.exports = connectDB;

I'm pretty sure is super easy to fix but since i'm newbie and i've been struggling all day trying to find solutions but just coudn't.

Can anyone help?

In the tutorial is used REST Client plugin. I tried to install it in intellij but got an error:

"Plugin "RESTClient" version was marked as incompatible."

Maybe thats why the api.http is red

api.http

So i don't know if is there other plugin or other way to use it and make it work.

Danka
  • 11
  • 3

0 Answers0