忍者ブログ

プログラミングの練習

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

[PR]

×

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


Java try-with-reasouces


リソース付のtry を利用すると

例外処理を抜けるとき、リソースが自動的に

close()されます

ファイルの読み込みで利用したら

こんな感じです
public static void main(String... args) {

    try(FileReader infile = new FileReader("C://aa.txt")){
        int c ;

        while((c = infile.read()) != -1 ) {
            System.out.println((char)c ) ;
        }

    }catch(Exception e) {
        System.out.println(e.getMessage());
    }

 }




PR

Java try-with-reasouces


リソース付のtry を利用すると

例外処理を抜けるとき、リソースが自動的に

close()されます

ファイルの読み込みで利用したら

こんな感じです
public static void main(String... args) {

    try(FileReader infile = new FileReader("C://aa.txt")){
        int c ;

        while((c = infile.read()) != -1 ) {
            System.out.println((char)c ) ;
        }

    }catch(Exception e) {
        System.out.println(e.getMessage());
    }

 }





Node モジュールの利用




1.モジュール側(sample31.js)

var func1  = function () {
  console.log('Sample31') ;
  return 10 ;
};

module.exports = func1 ;


2.利用する側(sample3.js)

var func1 = require("./sample31.js") ;
 
console.log('Sample3  Started') ;
console.log(func1()) ;

3.実行

$ node sample3.js

Sample3  Started

Sample31

10





express-generator


1.インストール

$sudo npm -g install express-generator

2.サンプルの自動生成

$express

次のようなファイルが作成してされました

$ tree
.
├── app.js
├── bin
│   └── www
├── package.json
├── public
│   ├── images
│   ├── javascripts
│   └── stylesheets
│       └── style.css
├── routes
│   ├── index.js
│   └── users.js
└── views
    ├── error.jade
    ├── index.jade
    └── layout.jade

7 directories, 9 files


3.依存ファイルのインストール

$npm install

4.実行

$DEBUG=node2:* npm start

ブラウザーで

http://localhost:3000/

にアクセスすると、表示されました

http://localhost:3000/users



respond with a resource

が表示されました







Node express アクセスしてきたURLの取得


こんなコードを書きます

var express = require('express');
 
var app = express();

app.get("/hello", function(req, res) {
  res.send("hello");


  console.log(req.protocol);
  console.log(req.headers.host);
  console.log(req.url);
});


app.listen(3000,function() {
 console.log("started");
});

ブラウザで

http://localhost:3000/hello

にアクセスすると

http
localhost:3000
/hello

が表示されます