1. 程式人生 > >Node.js控制檯示例專案(一)

Node.js控制檯示例專案(一)

////app.js

'use strict';

console.log('Hello world>>>>>>>>>>>>>>>>>>>>>>>>>>');

var green = require('./hello');
green.hello();
green.hello2();

//>>>>>>>>>>>> test http
/*
var http = require('http');
var server = http.createServer(function (request, response) {
    // 將HTTP響應200寫入response, 同時設定Content-Type: text/html:
    //response.writeHead(200, { 'Content-Type': 'text/html' });
    // 將HTTP響應的HTML內容寫入response:
    response.end('<h1>Hello world!</h1>');
});

server.listen(5000);
*/
//>>>>>>>>>>>>>> test express
/*
var express = require('express');
var app = express();
app.get('/', function (req, res) {
    res.end("test http web");
});
app.listen(5000, function () {
    console.log('http server start');
});
*/
//>>>>>>>>>>>>* test koa
/*
var Koa = require('koa');
var app = new Koa();

app.use(async (ctx, next) => {
    ctx.response.type = 'text/plain';
    if (ctx.request.path === '/test') {
        ctx.response.body = 'TEST page';
    }
    else {
        ctx.response.body = "test koa";
    }
});

app.listen(5000);
*/
//>>>>>>>>>>>>* test koa-router
/*
const Koa = require('koa');
// 注意require('koa-router')返回的是函式:
const router = require('koa-router')();
const app = new Koa();

// add url-route:
router.get('/hello/:name', async (ctx, next) => {
    var name = ctx.params.name;
    ctx.response.body = `<h1>Hello get, ${name}!</h1>`;
});
router.post('/hello/:name', async (ctx, next) => {
    var name = ctx.params.name;
    ctx.response.body = `<h1>Hello post, ${name}!</h1>`;
});
router.get('/', async (ctx, next) => {
    ctx.response.body = '<h1>Index</h1>';
});

// add router middleware:
app.use(router.routes());
app.listen(5000);
console.log('app started at port 5000...');
*/
//>>>>>>>>>>>>>>>>>>>>>> test koa-bodyparser
/*
const Koa = require('koa');
// 注意require('koa-router')返回的是函式:
const bodyParser = require('koa-bodyparser');
const router = require('koa-router')();
const app = new Koa();

// add url-route:
router.post('/hello/', async (ctx, next) => {
    var name = ctx.request.body.name || '';
    ctx.response.body = 'test body parser name ' + name;
});

// add router middleware:
app.use(bodyParser());
app.use(router.routes());
app.listen(5000);
console.log('app started at port 5000...');
*/

console.log('end<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<');

//hello.js

'use strict'
var str = 'hello';
module.exports = {
    hello: function () {
        console.log('this is test ' + str);
    },
    hello2: function () {
        console.log('this is test222222 ' + str);
    }
}