Azure app architecure - step02

Make Azure Function in Local


requirement
Install Node.js
Install Visual Studio Code.

Open the VS Code. i set the path C:\dev\blog-backend\.

Install Azure Function in Extension.

After install, you can show Azure Extension in left bar like picture.
And click Sign in to Azure… for connect your subscription.

After Auth, i connected my subscription.

And Right Click, Click Create Function App in Azure
Then type name you want. i named user-blog_backend
and select language node.js 12


After a few minute, we check function app created under the picture.

And select Azure function you created, Click icon Lightning.
And select javascript, HTTP trigger.
Type the name you want. i named create-user.
Then select anonymous.
Finally, create default code like this picture.

We execute this code using press F5.

tTen logging endpoint in terminal.

I try GET method using browser. it’s working well.

And we change code, add file.
create-user/index.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
module.exports = async function (context, req) {
var mongoose = require('mongoose');
var User = require('../model/user_model.js');
var CONF = require('../Config.js');

mongoose.connect("mongodb://"+CONF.COSMOSDB_HOST+":"+CONF.COSMOSDB_PORT+"/"+CONF.COSMOSDB_DBNAME+"?ssl=true&replicaSet=globaldb", {
auth: {
user: CONF.COSMOSDB_USER,
password: CONF.COSMOSDB_PASSWORD
},
useNewUrlParser: true,
useUnifiedTopology: true,
retryWrites: false
})
.then(() => console.log('Connection to CosmosDB successful'))
.catch((err) => console.error(err));

const user = new User({
name: req.query.name,
age: req.query.age,
sex: req.query.sex
});

context.res = {
// status: 200, /* Defaults to 200 */
body: await user.save()
};
}

Create folder and file model/user_model.js.

1
2
3
4
5
6
7
8
9
10
var mongoose = require('mongoose');
var Schema = mongoose.Schema;

const User = new mongoose.Schema({
name: String,
age: Number,
sex: String
});

module.exports = mongoose.model('User', User);

Create file Config.js in root path.
You need to refer cosmosDB connection_string in portal.

1
2
3
4
5
6
7
module.exports = {
COSMOSDB_USER : <cosmos_user_name>,
COSMOSDB_PASSWORD : <cosmos_password>,
COSMOSDB_DBNAME : "user", # we create database before
COSMOSDB_HOST : <cosmos_host_name>,
COSMOSDB_PORT : 10255
}

And we add mongoose module.
Execute cmd in terminal.

1
npm install mongoose@5.11.8 --save

After install well, execute function press F5 in index.js.
And test the function like this.

And we check the result in portal.

And i made two data test02, test03 using exeucte API .

get user data

We create additional function named find-user.
and change code
find-user\index.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
module.exports = async function (context, req) {
var mongoose = require('mongoose');
var User = require('../model/user_model.js');
var CONF = require('../Config.js');

mongoose.connect("mongodb://"+CONF.COSMOSDB_HOST+":"+CONF.COSMOSDB_PORT+"/"+CONF.COSMOSDB_DBNAME+"?ssl=true&replicaSet=globaldb", {
auth: {
user: CONF.COSMOSDB_USER,
password: CONF.COSMOSDB_PASSWORD
},
useNewUrlParser: true,
useUnifiedTopology: true,
retryWrites: false
})
.then(() => console.log('Connection to CosmosDB successful'))
.catch((err) => console.error(err));

context.res = {
body: await User.find()
};
}

Then, test the function like this.

We finish make Azure Function App in local.
Good job!!

Azure app architecure - Summary
Azure app architecure - step01
Azure app architecure - step03
Azure app architecure - step04
Azure app architecure - step05
Share