Getting a 404 when using http.request inside Defold

Hi. I’m new to defold and a newbie regarding REST services. I’m trying to migrate my game from Unity to Defold. Currently I’m trying to get a http respsone to work in defold. It’s working when I’m using the browser or Postman. But here I get a 404 error. Has anyone else had similar problems? Am I missing something? Thanks :slight_smile:

This is the Defold part:

-- send inputs to the server
function on_input(self, action_id, action)
	if action_id == hash("mine") and action.pressed then		
		example(self)
	end
end

function http_result(self, id, response)
    print(response.status)
    print(response.response)
    pprint(response.headers)
end

function example(self)
    http.request("http://localhost:8080/api/users/", "GET", http_result)
end

This is my server.js. I’m using Node.js for the server part. I have removed all the logic and it’s just a basic server so that it’s easier to read and understand.

// server.js

// call the packages we need
var express    = require('express');        // call express
var app        = express();                 // define our app using express
var bodyParser = require('body-parser');

// configure app to use bodyParser()
// this will let us get the data from a POST
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

var port = process.env.PORT || 8080;        // set our port

var router = express.Router();              // get an instance of the express Router

// middleware to use for all requests
router.use(function(req, res, next) {
    console.log('Something is happening.');
    next(); // make sure we go to the next routes and don't stop here
});

// test route to make sure everything is working (accessed at GET http://localhost:8080/api)
router.get('/', function(req, res) {
    res.json({ message: "server is working!"});
});

router.route('/users/')
    .get(function(req, res) {
        res.json({ message: "Hello" });
    });

// REGISTER OUR ROUTES-
// all of our routes will be prefixed with /api
app.use('/api', router);

// START THE SERVER
app.listen(port);
console.log('Magic happens on port ' + port);

Here is a picture showing that it’s working outside of Defold.

Me again :slight_smile:

I solved the problem by simply restarting Defold. However I got this error message when i opened my project.

Hey!

IIRC defold uses 8080 too, to do the communication between editor and running game/engine. It does not really explain why you would only get 404s from the engine, and not from the other software (could that be firewall?). But try to start your own server to another port and see what happens. The ports Defold uses should obviously be configurable, but it’s currently not, sorry.

2 Likes