Deploy node js REST Api on production and fetch data using react native

i am new to node js. i am learning to create REST api's on node js. i create a basic file in node js and run it on my local machine it is working fine and i test it using the postman and it giving me the results as well. Here is the code

const express = require('express'); //for routes
const app = express();
app.use(express.json());

// creating an array of courses
const courses = [
    { id: 1, name: 'courses1' },
    { id: 2, name: 'courses2' },
    { id: 3, name: 'courses3' },
]

// getting specific course
app.get('/api/courses/:id', (req, res) => {
    const course = courses.find(c => c.id === parseInt(req.params.id));
    if (!course) return res.status(404).send('the course with the given id not found.');
    res.send(course);
});

// post course data 
app.post('/api/courses', (req, res) => {

    const { error } = validateinputs(req.body);

    if (error) {
        res.status(400).send(error.details[0].message);
        return;
    }

    const course = {
        id: courses.length + 1,
        name: req.body.name
    }

    courses.push(course);
    res.send(course);
});

// running on port.
const port = process.env.PORT || 3000;
app.listen(port, () => console.log('Listening on port ' + port));

This is the basic code i have tested on my local machine with postman and its working. But now i want it to deploy on my server. i have installed node and npm using server console. and copy this file inside my new created project on production server. Like this.

const express = require('express'); //for routes

const app = express();
app.use(express.json()); // for post

// creating an array of courses
const courses = [
    {id: 1, name: 'courses1'},
    {id: 2, name: 'courses2'},
    {id: 3, name: 'courses3'},
]

app.get('/api', (req, res) => {
    res.send('Hey');
});

// res.send(courses);

// getting specific course
app.get('/api/courses/:id', (req, res) => {
    const course = courses.find(c => c.id === parseInt(req.params.id)
);

if (!course) return res.status(404).send('the course with the given id not found.');
res.send(course);

});

// running on port.
const port = process.env.PORT || 3000;
app.listen(port, () => console.log('Listening on port ' + port));

then i start the server manually on production server like

node index.js

and its giving me the message

Listening on port 3000

Now when i testing it with postman its not working fine. I am using this link where the project is installed under the folder adroit_api.

https://adroit-services.co.uk/adroit_api/api

It is returning me the source page of this site home page

https://adroit-services.co.uk

i do not know what to do. please help.

Thanks

1 answer

  • answered 2019-01-11 05:53 ionizer.aj

    You may have routing issues outside your NodeJS Express app. Check your web server's settings like virtual host.

    That being said, your API runs on port 3000 whereas your web server is on port 80. So, you might want to check your API at something like https://example.com:3000/api as well (replace example.com with your own server's URL), as accessing https://example.com/api means you're trying to access it on port 80 instead of 3000.

    Also, I can read your server-sided code if you put them inside your web server's directory by accessing https://example.com/example_api/index.js, which will very likely cause very serious security breach.