Wednesday, January 29, 2025

Express (node.js)

Express.js:

Node.jsのための軽量で柔軟なWebアプリケーションフレームワークで、Express.jsを使用すると、Node.jsを使用してWebアプリケーションを作成するための様々な機能を簡単に実装できるそうなので使ってみた。

インストールは npm で簡単:

Express can use a template engine to render dynamic pages with information that differs with each request.

There are several engines to choose from but let's start with EJS.

First install EJS in your project:

npm install ejs

番外:
nodemon 使うと、ファイル更新毎にサービス再起動しなくていいので便利。
npm install nodemon
npx nodemon index.js

Sunday, January 26, 2025

BootCamp

 Windows デスクトップ・マシンを奥様に奪われてしまったので、古い Mac を Windows マシンとして使うことにしました。最後の Intel チップの Mac ですが中身は PC ですから、BootCamp を使えばネイティブで走る Windows マシンに。

Intel チップの Mac は実はこれが 3 台目で、過去にも Windows を使わないといけないというシチュエーションはありましたので、BootCamp で Windows はインストールしたことはありましたが、10 年以上も前なので覚えていません。

その時は、自分が持っている Windows 7 を先にインストールして Windows 10 ISO ファイルをダウンロードして 10 にアップグレードした記憶があります。

確か、起動時に Command (Apple Key) + R でリカバリーモードにして、ディスクユーティリティからパーティション切って、USBメモリから起動して、外付け CD ROM ドライブから Windows 7 CD を使ってインストールしたと思うのですが、今回、Mac の BootCamp アシスタント使ったら簡単にインストールが完了しましたので今回はここに備忘録として残しておきます

インストール手順:

  • 新しい Mac にデータを移行は完了済みなので、リカバリーモードからパーティションを作りなおした(この時点では全てのスペースを一つパーティションで Mac のみ)。
  • 真っ新のパーティションに Mac OS をクリーンインストール。
  • Mac OS のインストールが終わったら最新版になるまでアップデートをあてる。
  • Windows 10  の ISO イメージをダウンロード:
  • 下記リンクが参考になったが、サファリでは普通にダウンロードできた。https://www.youtube.com/watch?v=EBoCZc5XGrA
  • ユーティリティの中に BootCamp アシスタントを立ち上げ、Windows OS に割り当てるパーティションのサイズを指定して、ダウンロードした ISO イメージを選択したら、後は普通の Windows のインストールと同じ(簡単になった!)。
  • インストール中、当然、ライセンスを聞いてくるが「I do not have ...」オプションを選んでもインストールは続行できて完了してしまった・・・(大丈夫なのだろうか?笑)。ドライバーなどハードに必要なソフトも勝手にインストールされている!(驚)。参照:https://youtu.be/NKCi7vALgJQ
  • Windows も最新版になるまで Windows Update をあてる。

インストール完了後:

起動時(電源入れたとき)、どちらかのOSを選ぶには Option キーを押しながら起動。

Sunday, January 19, 2025

Mac のバックアップ

 

久しぶりに Mac の入れ替え。今使っている MacBook Pro は 10 年以上かも。。。
宗教上の理由(笑)で Intel チップ Mac を使い続けていましたが、限界。。。
廉価版 Air に 15 インチがあるので、性能よりも画面サイズ重視。



Mac の移行は古い Mac の横に、新品並べて電源入れると自動で同期してくれるのですが、古いアプリとかゴミとかも移行されたら嫌なのでシンクはしないで手動で必要なファイルだけを動かしました。

Apple 純正 App:

Contacts, Reminder, Calender, KeyChain 等、普段から iPhone ともクラウドで共有しているものは移行する必要はなし。

Notes はローカルにもあるので、それは手動で移行:

~/Library/Group Containers/group.com.apple.notes から

  • NoteStore.sqlite
  • NoteStore.sqlite-shm
  • NoteStore.sqlite-wal
  • group.com.apple.notes/Accounts/LocalAccountからMedia(画像含む場合)
  • group.com.apple.notes/Accounts/LocalAccountからPreview(画像含む場合)

上記ファイルを抜き出して、新しいMacに移動させれば、完了。

*~/Library は隠しファイルなので、Mac の場合は 「command」+「shift」+「.」 で、隠しファイルが表示されます。

ファイル:

home の下、Desktop, Documents, Downloads, Movies, Music, Pictures をコピー。

その他:

サファリと Chrome の BookMark をエクスポート。

番外:

なんと!子供が9歳の誕生日の時にプレゼントした iPod クラシック(エングレービングされてた 笑)が捨てられていたので、外付け HDD として使ってみたらイケてる!

https://av.watch.impress.co.jp/docs/topic/1390798.html

追記:

mv -v ~/Library/Containers/com.apple.FinalCutTrial/Data/Library/Application\ Support/.ffuserdata ~/.Trash


Saturday, November 30, 2024

Node.js

 ブラウザーを使わないでJavaScriptを実行できる環境(だと理解してますが、、、間違っているかも・・・)Node.js というのを試してみた。

最近はWeb開発や自動化などに利用されることが多いとか。

Node.js is a run-time engine that executes JavaScript code outside a browser. Originally intended as a web server, but also commonly used for web development tools and automation.

https://nodejs.org/en/からWindows版をダウンロードして全てディフォルトでインストール:


インストール後、コマンドラインでインストールされているか確認(バージョン情報がでました)。

簡単なWebサーバーを立ち上げ:

import http from 'http';

http.createServer((req,res) => {

    res.writeHead(200, {'Content-Type': 'text/plain'});

    res.end('Aloha world');

}).listen(process.env.PORT || 3000);

node.js をインストールすると、npm というパッケージ化ユーティリティ(だと思う。。。)が入っていたので、npm init でパッケージも作成してみた。

package.json

{
  "name": "ほげほげ",
  "version": "1.0.0",
  "description": "demo node application",
  "main": "index.js",
  "type": "module",
  "scripts": {
  "start": "node index.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/tou-shiro/"
  },
  "author": "ほげ~",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/tou-shiro/"
  },
  "homepage": "https://github.com/tou-shiro/"
}

index.js

import http from 'http';
http.createServer((req,res) => {
    var path = req.url.toLowerCase();    
    switch(path) {
        case '/':
            res.writeHead(200, {'Content-Type': 'text/plain'});
            res.end('Home page');
            break;
        case '/about':
            res.writeHead(200, {'Content-Type': 'text/plain'});
            res.end('About page');
            break;
        default:
            res.writeHead(404, {'Content-Type': 'text/plain'});
            res.end('Not found');
            break;
    }    
}).listen(process.env.PORT || 3000);

コマンドラインから下記のように走らせる:

Node.js can also execute JS files from the command line like:

$ node index.js

http://localhost:3000でつないでみたら、正しく表示されたので動いていることを確認できました。

Monday, November 25, 2024

systemd vs initd

昔はサービス再起動は service restart httpd のようにしていたが、いつのまにか sudo systemctl restart httpd という風に変わっていたので、いつ変わったのか AI に聞いてみた。


The change from the service command to systemctl in Linux occurred with the adoption of systemd as the default init system, replacing the older SysVinit system.

Timeline of Change:

  • Systemd Introduction: systemd was first introduced in 2010 by Lennart Poettering and Kay Sievers as an alternative to SysVinit.
  • Adoption in Major Distributions:
    • Fedora became the first major Linux distribution to adopt systemd as its default init system in Fedora 15 (released in May 2011).
    • Debian switched to systemd as the default init system starting with Debian 8 (Jessie), released in April 2015.
    • Ubuntu started using systemd as the default init system with Ubuntu 15.04 (released in April 2015).

Why the Change?

The change to systemctl was made to take advantage of the features and improvements that systemd brings over the older SysVinit system, including:

  • Parallel service startup (faster boot times).
  • More powerful service management with systemctl.
  • Better tracking and logging with journalctl.
  • Better handling of dependencies between services.

While service was part of the older SysVinit system, systemctl is part of the systemd system and is now the recommended tool for managing services on most modern Linux distributions.

Compatibility:

  • In many distributions that have switched to systemd, the service command is still available as a wrapper around systemctl for backward compatibility. However, using systemctl directly is the preferred method now.

Saturday, November 23, 2024

LeetCode 567 Permutation in String

コミュカレでイントロ Java の授業を取っていてだされた宿題に LeetCode 567 Permutation in String に近いものがあったのでトライしてみた。

11.2.1: LAB: All permutations of names

Write a program that lists all ways people can line up for a photo (all permutations of a list of Strings). The program will read a list of one word names into ArrayList nameList (until -1), and use a recursive method to create and output all possible orderings of those names separated by a comma, one ordering per line.

import java.util.Scanner;
import java.util.ArrayList;

public class PhotoLineups {

   // Method to create and output all permutations of the list of names
   public static void printAllPermutations(ArrayList<String> permList, ArrayList<String> nameList) {
      // Base case: when nameList is empty, print the current permutation
      if (nameList.size() == 0) {
         System.out.println(String.join(", ", permList));
         return;
      }

      // Iterate through the list and recursively generate all permutations
      for (int i = 0; i < nameList.size(); i++) {
         // Select the current name
         String currentName = nameList.get(i);

         // Create a new list of remaining names by removing the current name
         ArrayList<String> remainingNames = new ArrayList<>(nameList);
         remainingNames.remove(i);

         // Add the current name to the permutation list
         permList.add(currentName);

         // Recurse with the remaining names and current permutation list
         printAllPermutations(permList, remainingNames);

         // Backtrack: remove the last added name to try the next permutation
         permList.remove(permList.size() - 1);
      }
   }

   public static void main(String[] args) {
      Scanner scnr = new Scanner(System.in);
      ArrayList<String> nameList = new ArrayList<>();
      ArrayList<String> permList = new ArrayList<>();
      String name;

      // Read names from the user until "-1" is entered
      while (true) {
         name = scnr.next().trim();
         if (name.equals("-1")) {
            break;  // Stop reading names when "-1" is encountered
         }
         nameList.add(name);  // Add name to the list
      }

      // Generate and print all permutations of the list of names
      printAllPermutations(permList, nameList);
   }
}

Tuesday, October 22, 2024

replit

 replit というブラウザ・ベースの IDE を使う機会があったので備忘録として。

元々、スマフォでもコーディングしたいということでスタートしたらしいですが、AI もインテグレートされていて便利ですな。

新規で Repl を作成:

環境を選択:

普通に使えます。Git 環境もあり:


別に自分の PC のローカル開発環境でも十分ですが、何も構築しないでブラウザーですぐに始められるというのはGood:

自分の リモートGithubを指定:

勝手に repo 作ってくれます:

Git add、commit、push もコマンドでなくても可能:


でも、サブスクライブ月々 $25 だって。。。

自分のローカルPC環境で十分です。。。