0
votes

I am trying to connect to an API that I have up and running on my local machine but I can't access on my production server.

It's running on node.js with mongoose and I have set up the server to listen on port 3000.

When I test in postman: http://178.128.37.170:3000/

I get the following response:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <title>Error</title>
</head>

<body>
    <>Cannot GET /</>
</body>

</html> 

I have created a 'Hello world' response in the router: app.get('/', (req, res) => res.send('Hello World!'))

I am not sure where this is getting blocked, if it is a port, firewall or IP issue or something else. I have disabled both Apache and SSL in case they were interfering.

Here is my server.js file:

var express = require('express'),
    cors = require('cors'),
    app = express(),
    port = process.env.PORT || 3000,
    mongoose = require('mongoose'),
    Supporter = require('./api/models/todoListModel'), //created model loading here
    bodyParser = require('body-parser'),
    helmet = require('helmet');

// Test SSL connection
var MongoClient = require('mongodb').MongoClient;

// mongoose instance connection url connection
mongoose.Promise = global.Promise;
mongoose.connect('mongodb://localhost/Tododb');  // was tododb

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

app.use(helmet());

app.use(cors());

app.get('/task/', function (req, res, next) {
    res.json({msg: 'This is CORS-enabled for all origins!'})
})

var routes = require('./api/routes/todoListRoutes');
routes(app); //register the route

app.listen(port);

console.log('Supporter RESTful API server started on: ' + port);

What should I try next?

1
app.get('/', ...) does not appear in your server.js Can you access /task from Postman? - Samuel
I noticed that this line Supporter = require('./api/models/todoListModel'), //created model loading here was incorrect so I changed that and I can now get a response from /task but I am still getting no response from /tasks/1 which is where my routes are pointing. - PhilipAbbott
Add the file with handlers too. Also, there's a bit of a shift here from your original question, previously you cldnt access / and now it's task/1 - Samuel

1 Answers

0
votes

I fixed this. Somehow, my Mongo database had been deleted from my server but that's another story!

In my server.js, I added this: app.get('/', (req, res) => res.send('Hello World!'))

That allowed me to work out that express was serving the endpoint.

Then I rang mongo and recreated the database, and eventually got to the routes working.