1
Help us understand the problem. What are the problem?

More than 3 years have passed since last update.

posted at

amazon linuxでNode.jsのNEM開発環境を爆速で整える

pythonでのNEM開発環境を説明しましたので、次はNode.jsの開発環境の説明です。
今回はサーバを立ち上げてURLでアクセスするとアカウントを作成するところまで行きます。

nvmをインストールします。

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" 

node.js のインストール

nvm install v8.11.3

必要ライブラリのインストール

npm install nem-library
npm install url
npm install express
npm install body-parser

サーバプログラムです。
nem-libraryをjsで書く方法はDaokaさんに教えていただきました。
TypeScriptじゃなくても動きます。

node.js

const url        = require('url');
const express    = require('express');
const bodyParser = require('body-parser');
const nem        = require("nem-sdk").default;

var app = express();
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
app.use(function(req, res, next){
    res.header("Access-Control-Allow-Origin","*");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    next();
})

app.listen(1337);

const nemlibrary = require('nem-library');
const NetworkTypes = nemlibrary.NetworkTypes, Password = nemlibrary.Password, SimpleWallet = nemlibrary.SimpleWallet;
nemlibrary.NEMLibrary.bootstrap(NetworkTypes.TEST_NET);

app.get('/account', (req, res) => {

    const password = new Password("password");
    const simpleWallet = SimpleWallet.create("simple wallet", password);

    res.writeHead(200, {'Content-Type': 'text/plain; charset=utf-8'});
    res.write(simpleWallet.address.pretty());
    res.end();
});

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Sign upLogin
1
Help us understand the problem. What are the problem?