Skip to Content
Technical Articles
Author's profile photo Yohei Fukuhara

OData(get)をNode.js(express)で呼び出すサンプル on SAP CP CF

OData(get)をNode.js(express)で呼び出すサンプルアプリをSAP CP CFにデプロイしました。

GitHubにコードを置いています。

前提

開発PC

  • Ubuntu18.04.01 LTS on VMWare Workstation
  • cf CLI 6.49.0 インストール済
  • node.js 12.14.1(nvmで管理) インストール済
  • npm 6.13.4 インストール済

Cloud Foundry

  • Nodejs Buildpack version 1.7.4
  • CF Trial (Europe – Frankfurt)

ステップ

1. Node.js開発

1.1. プロジェクト作成

ローカルPCにプロジェクトフォルダ”node-cf-rest”を作成

touch node-cf-rest

1.2. npmのセットアップ

ディレクトリを”node-cf-rest”に移動してnpmのセットアップ(GitでCloneしてから実施しているので、git repositoryと紐付いてます)。

$ cd node-cf-rest
$ npm init

This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help json` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
package name: (cf-rest) node-cf-rest
version: (1.0.0) 
description: Node.js consuming REST API on Cloud Foundry
entry point: (index.js) 
test command: 
git repository: (https://github.com/YoheiFukuhara/node-cf-rest.git) 
keywords: 
author: 
license: (ISC) 
About to write to /home/i348221/Apps/node/node-cf-rest/package.json:

{
  "name": "node-cf-rest",
  "version": "1.0.0",
  "description": "Node.js consuming REST API on Cloud Foundry",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/YoheiFukuhara/node-cf-rest.git"
  },
  "author": "",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/YoheiFukuhara/node-cf-rest/issues"
  },
  "homepage": "https://github.com/YoheiFukuhara/node-cf-rest#readme"
}


Is this OK? (yes) yes

1.3. パッケージインストール

express(4.17.1)とrequest(2.88.0)をインストール

npm install --save express request

1.4. index.js作成

プロジェクトディレクトリ直下に”index.js”を作成。

※以下の通信先およびユーザ・パスワードを変更してください。

// Import modules
const express = require('express')
const request = require('request')

const app = express()
const port = process.env.PORT || 3000;;

// Get method
app.get('/', (req, res) => res.send('Hello World! for GET'))

app.get('/api', function (req, res) {
  
  request.get({
    uri: 'http://<host>:<port>/sap/opu/odata/sap/CB_CUSTOMER_SRV/Customers?$format=json',
    headers: {'Content-type': 'application/json'},
    auth: {
        'user': '<user>',
        'pass': '<password>',
        'sendImmediately': false
    }
}, function(err, req, data){
    res.status(200).json(data)
    console.log(data);
});
});

// Listen 
app.listen(port, () => console.log(`Example app listening on port ${port}!`))

 

1.5. package.json変更

package.jsonの開始スクリプトを変更

{
  "name": "node-cf-rest",
  "version": "1.0.0",
  "description": "Node.js consuming REST API on Cloud Foundry",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/YoheiFukuhara/node-cf-rest.git"
  },
  "author": "",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/YoheiFukuhara/node-cf-rest/issues"
  },
  "homepage": "https://github.com/YoheiFukuhara/node-cf-rest#readme",
  "dependencies": {
    "express": "^4.17.1",
    "request": "^2.88.0"
  }
}

1.6. ローカルテスト

ローカルで起動してテスト。

$ node index.jp
Example app listening on port 3000!

ブラウザから”http://localhost:3000“にアクセスすると”Hello World! for GET”と表示され、”http://localhost:3000/api“にアクセスするとodataで取得したjsonが表示されます。

2. SAP CP CF Appにデプロイ

2.1. manifest.yaml作成

プロジェクトディレクトリ直下にmanifest.yamlを作成

---
applications:
- name: node-rest
  random-route: true
  memory: 128M

2.2. cf にログイン・デプロイ

cf にログインしてpushでデプロイします。

cf login
cf push node-rest

デプロイに成功すれば、cf側で作られたurlに対してブラウザアクセスすることで1.6と同じ結果が見られます。

Assigned Tags

      Be the first to leave a comment
      You must be Logged on to comment or reply to a post.