如何使用Express导出不同的功能

时间:2019-07-30 14:27:16

标签: node.js google-cloud-functions

我将此代码用于托管在Google Cloud存储库中的存储库。

我将此代码用于Google Cloud Functions的某些功能。

我想为不同的功能使用相同的存储库。 我希望这是可能的:

功能1(帐户)

  • 帐户/用户
  • 帐户/用户/:userId

功能2(信息)

  • 信息/评论
  • info / comments /:commentId

这是我的代码:

const express = require('express');
const cors = require('cors');
const account = express();
const info = express();

account.use(cors({
    origin: '*'
}));
info.use(cors({
    origin: '*'
}));

const PORT = 5555;

const PORT2 = 5556;

const USERS = [
    {
        id: 1,
        firstName: 'John',
        lastName: 'Smith'
    },
    {
        id: 2,
        firstName: 'Jane',
        lastName: 'Williams'
    }
];

const COMMENTS = [
    {
        id: 1,
        firstName: 'John',
        lastName: 'Smith'
    },
    {
        id: 2,
        firstName: 'Jane',
        lastName: 'Williams'
    }
];

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

account.get('/users', (req, res, next) => {
    res.json(USERS);
});

account.get('/users/:userId', (req, res, next) => {
    res.json(USERS.find(user => user.id === parseInt(req.params.userId)));
});

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

info.get('/comments', (req, res, next) => {
    res.json(COMMENTS);
});

info.get('/comments/:commentId', (req, res, next) => {
    res.json(COMMENTS.find(comment => comment.id === parseInt(req.params.commentId)));
});

module.exports = {
    account,
    info
};

为此,我可以使用相同的仓库部署不同的功能帐户信息。 但是我不想实例化两个我认为不好的表达对象。

我需要为不同的Google Cloud Functions导出两个函数。 我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:1)

最好组织代码。创建app.js文件包含以下代码。使用一个端口。

const express = require('express');
const cors = require('cors');
const app = express();
const users = require('./users');
const comments = require('./comments');

app.use(cors({
    origin: '*'
}));
app.use('/users', users);
app.use('/comments', comments);

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

/// place this in users.js    

const express = require('express');
const account = express.router;

account.get('/', (req, res, next) => {
    res.json(USERS);
});

account.get('/:userId', (req, res, next) => {
    res.json(USERS.find(user => user.id === parseInt(req.params.userId)));
});

module.exports = account;

// place in comments.js
const express = require('express');
const info= express.router;

const COMMENTS = [
    {
        id: 1,
        firstName: 'John',
        lastName: 'Smith'
    },
    {
        id: 2,
        firstName: 'Jane',
        lastName: 'Williams'
    }
];

info.get('/', (req, res, next) => {
    res.json(COMMENTS);
});

info.get('/:commentId', (req, res, next) => {
    res.json(COMMENTS.find(comment => comment.id === parseInt(req.params.commentId)));
});

module.exports = info;

答案 1 :(得分:0)

const express = require('express');
const cors = require('cors');
const app = express();


app.use(cors({
    origin: '*'
}));

const PORT = 5555;

const USERS = [
    {
        id: 1,
        firstName: 'John',
        lastName: 'Smith'
    },
    {
        id: 2,
        firstName: 'Jane',
        lastName: 'Williams'
    }
];

const COMMENTS = [
    {
        id: 1,
        firstName: 'John',
        lastName: 'Smith'
    },
    {
        id: 2,
        firstName: 'Jane',
        lastName: 'Williams'
    }
];



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

app.get('/users', (req, res, next) => {
    res.json(USERS);
});

app.get('/users/:userId', (req, res, next) => {
    res.json(USERS.find(user => user.id === parseInt(req.params.userId)));
});


app.get('/comments', (req, res, next) => {
    res.json(COMMENTS);
});

app.get('/comments/:commentId', (req, res, next) => {
    res.json(COMMENTS.find(comment => comment.id === parseInt(req.params.commentId)));
});

module.exports = app;