简体   繁体   English

在生产环境中部署 node js REST Api 并使用 react native 获取数据

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

i am new to node js.我是 node js 的新手。 i am learning to create REST api's on node js.我正在学习在节点 js 上创建 REST api。 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.我在 node js 中创建了一个基本文件,并在我的本地机器上运行它,它工作正常,我使用邮递员测试它,它也给了我结果。 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.我已经使用服务器控制台安装了 node 和 npm。 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.我正在使用此链接,其中项目安装在文件夹 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谢谢

You may have routing issues outside your NodeJS Express app.您可能在 NodeJS Express 应用程序之外遇到路由问题。 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.也就是说,您的 API 在端口 3000 上运行,而您的 Web 服务器在端口 80 上。因此,您可能还想在https://example.com:3000/api类的地方检查您的 API(将example.com替换为您自己的服务器的 URL),因为访问https://example.com/api意味着您尝试在端口 80 而不是 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.此外,如果您通过访问https://example.com/example_api/index.js将它们放在 Web 服务器的目录中,我可以读取您的服务器端代码,这很可能会导致非常严重的安全漏洞。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM