LoginSignup
6
1

More than 5 years have passed since last update.

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

Posted at

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();
});

6
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
1