忍者ブログ

プログラミングの練習

プログラミングの問題やプログラミング関連知識、ソフトウェアのテストについてのブログです

[PR]

×

[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。


node モジュールの作成

1. サンプル

呼ぶ方(main.js)
var module1 = require('./module1.js');
console.log(module1.timeStamp());

呼ばれる方(module1.js)
const timeStamp = () => {
    const date = new Date();
    return date.getTime();
};

module.exports = {
    timeStamp
};


2.実行結果

1621842316452

などのtimestampが表示されます。


PR

node for での出力

1. サンプル


次の内容のファイルを作成します(sample3.js)

for( var i = 0 ; i < 10 ; i++)
{
  console.log(i) ; 
 }

2.実行結果


作成したプログラムを、次のように実行します

・・・>node sample3

実行したターミナルに

0
1
2
3
4
5
6
7
8
9

が出力されます 





AWS SDK for Pythonのインストール

ubuntu 18.04.3 で試しています

1 インストール

install boto3

2 設定

AWS CLI のインストール

での設定をそのまま利用します

3 確認

次のような、S3バケットの一覧を表示するプログラムを実行してみて確認しました
import boto3

s3 = boto3.resource('s3')
for bucket in s3.buckets.all():
    print(bucket.name)




node イベントループ

10秒待って、メッセージを出す処理です

1.作成


次の内容のファイルを作成します(sample4.js)
setTimeout( function() {
 console.log("TimeOut occurred")
}, 1000) ;

console.log("Waiting ・・・") ;

Waiting ・・・と出力し、10秒経つと

TimeOut occurredを出力する処理です

2.実行


作成したプログラムを、次のように実行します

・・・>node sample4


実行したターミナルに

Waiting ・・・
TimeOut occurred

と出力されます




node express Router の利用


URLと、その対応を別ファイルに分離します

1.ルーティング情報(router.js)


var express = require('express');
var router = express.Router() ;

router.get('/', function(req, res){
  res.send("Hello world!")
});


module.exports = router ;

2.メイン処理


var express = require('express');

var app = express();

var router = require('./router.js')  ;

// /app は、routerによって、ルーティングされます

app.use('/app',router)


app.listen(3000);


3.実行

http://localhost:3000/app



Hello world!

が出力されます


        
  • 1
  • 2