nodejs - hello world

Node

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
var http = require("http");

http.createServer(function (req, res) {

// Send the HTTP header
// HTTP Status: 200 : OK
// Content Type: text/plain
res.writeHead(200, {'Content-Type': 'text/plain'});

// Send the res body as "Hello World"
res.end('Hello World\n');
}).listen(3000);

// Console will print the message
console.log('Server running at http://127.0.0.1:3000/');

Node + Express

index.js file

1
2
3
4
5
6
7
8
9
10
let express = require("express");
let app = express();

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

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

ES6

1
2
3
4
5
6
const express = require('express');
const app = express();

app.get('/', (req, res) => res.send('Hello World!'));

app.listen(3000, () => console.log('Example app listening on port 3000!'));

nodejs - init

  1. 安裝node.js

  2. 建立node專案資料夾 (e.g. d:\server)

  3. 使用命令介面操作
    3.1 Windows:命令提示字元(cmd)
    3.2 Mac: 終端機
    3.3 注意[工作路徑]

  4. 切換到工作資料夾
    4.1 切換磁碟機:磁碟機代號
    4.2 切換資料夾:cd 資料夾路徑

  5. 執行 Javascript 程式: node 程式檔案

  6. 使用 npm 套件管理工具
    6.1 初始化 Node 專案:

1
npm init
  1. 安裝 express 架設網站套件
1
npm install express --save
  1. 撰寫第一支伺服器程式

  2. 安裝ejs

    1
    npm insall ejs --save

Refs padaLab@gmail.com

hello_world

python

1
print('hello world')

julia

1
println("hello world")

ruby

1
p 'hello world'

r

1
print('hello world')

matlab

1
disp('hello world')

c

1
2
3
4
5
6
7
#include <stdio.h>
#include <stdlib.h>
int main(int argc,char** argv)
{
printf("hello world\n");
return 0;
}

c++

1
2
3
4
5
6
7
#include <iostream>
using namespace std;
int main(int argc,char *argv[])
{
cout<<"hello world"<<endl;
return 0;
}

java

1
2
3
4
5
public class hw {
public static void main(String[] args) {
System.out.println("hello world");
}
}

C Sharp

1
2
3
4
5
6
7
8
9
10
11
12
using System;

namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("hello world");
}
}
}

help

General

Image

Documentations