Node.js控制台示例项目(一)
【摘要】
app.js'use strict'; console.log('Hello world>>>>>>>>>>>>>>>>>>>>>>>>>>'); var green = requi...
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);
}
}
文章来源: zzzili.blog.csdn.net,作者:清雨小竹,版权归原作者所有,如需转载,请联系作者。
原文链接:zzzili.blog.csdn.net/article/details/79305307
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
作者其他文章
评论(0)