├── .gitattributes ├── server ├── hello_server.ts └── auction_server.ts ├── .idea ├── vcs.xml ├── modules.xml ├── typescript-compiler.xml ├── server.iml └── workspace.xml ├── README.md ├── tsconfig.json └── package.json /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto -------------------------------------------------------------------------------- /server/hello_server.ts: -------------------------------------------------------------------------------- 1 | import * as http from 'http' 2 | 3 | const server = http.createServer((request,response)=>{ 4 | response.end("Hello Node"); 5 | }); 6 | server.listen(8000); -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # nodejs搭一个web服务器 2 | 3 | 新建server文件夹 4 | 5 | 初始化 npm init -y 6 | 7 | 引入类型定义文件 npm i @types/node --save 8 | 9 | 编写tsconfig配置文件 10 | 11 | 编写80端口hello.ts文件 12 | 13 | 启动 node build/hello_server.js 14 | 15 | 16 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "es5", 4 | "module": "commonjs", 5 | "emitDecoratorMetadata": true, 6 | "experimentalDecorators": true, 7 | "outDir": "build", 8 | "lib": ["es6"] 9 | }, 10 | "exclude": [ 11 | "node_modules" 12 | ] 13 | } -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/typescript-compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 8 | -------------------------------------------------------------------------------- /.idea/server.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "server", 3 | "version": "1.0.0", 4 | "description": "nodejs搭一个web服务器", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/hzlshen/server.git" 12 | }, 13 | "keywords": [], 14 | "author": "", 15 | "license": "ISC", 16 | "bugs": { 17 | "url": "https://github.com/hzlshen/server/issues" 18 | }, 19 | "homepage": "https://github.com/hzlshen/server#readme", 20 | "dependencies": { 21 | "@types/express": "^4.0.39", 22 | "@types/node": "^8.0.54", 23 | "express": "^4.16.2", 24 | "ws": "^3.3.2" 25 | }, 26 | "devDependencies": { 27 | "@types/ws": "^3.2.1" 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /server/auction_server.ts: -------------------------------------------------------------------------------- 1 | import * as express from 'express'; 2 | import {Server} from "ws"; 3 | 4 | const app = express(); 5 | 6 | export class Product{ 7 | constructor( 8 | public id:number, 9 | public title:string, 10 | public price:number, 11 | public rating:number, 12 | public desc:string, 13 | public categories:Array 14 | ){ 15 | 16 | } 17 | } 18 | 19 | export class Comment{ 20 | constructor( 21 | public id :number, 22 | public productId : number, 23 | public timestamp:string, 24 | public user: string, 25 | public rating:number, 26 | public content:string 27 | ){ 28 | 29 | } 30 | } 31 | 32 | const products:Product[]=[ 33 | new Product(1,"第一个商品",1.99,2.5,"这是一个商品Helloo word",["电子产品","硬件设备"]), 34 | new Product(2,"第二个商品",8.49,4.5,"这是二个商品Helloo word",["图书","硬件设备"]), 35 | new Product(3,"第三个商品",7.09,1.5,"这是三个商品Helloo word",["服装","硬件设备"]), 36 | new Product(4,"第四个商品",6.39,2.5,"这是四个商品Helloo word",["线下","硬件设备"]), 37 | new Product(5,"第五个商品",5.49,2.5,"这是五个商品Helloo word",["你好","硬件设备"]), 38 | new Product(6,"第六个商品",3.69,3.5,"这是六个商品Helloo word",["电子产品","硬件设备"]) 39 | ]; 40 | 41 | const comments:Comment[] = [ 42 | new Comment(1,1,"2017-02-20 22:13:22","张三",3,"东西不错"), 43 | new Comment(2,1,"2017-01-25 23:23:22","李四",4,"东西是不错"), 44 | new Comment(3,1,"2017-03-10 21:29:22","王五",2,"东西挺不错"), 45 | new Comment(4,2,"2017-04-06 20:22:22","隔壁",5,"东西可以不错"), 46 | ]; 47 | 48 | 49 | app.get('/',(req,res)=>{ 50 | res.send("Hello Express"); 51 | }); 52 | 53 | // 所有商品 54 | app.get("/api/products",(req,res)=>{ 55 | res.json(products); 56 | }); 57 | 58 | // 商品 带id 59 | app.get("/api/product/:id",(req,res)=>{ 60 | res.json(products.find((product)=>product.id ==req.params.id)); 61 | }); 62 | 63 | // 评论 64 | app.get("/api/product/:id/comments",(req,res)=>{ 65 | //comments.filter((comment:Comment)=>comment.productId ==id); 66 | res.json(comments.filter((comment:Comment)=>comment.productId ==req.params.id)); 67 | }); 68 | 69 | const server = app.listen(8000,"localhost",()=>{ 70 | console.log("服务器已启动,地址是:http://localhost:8000"); 71 | }) 72 | 73 | const wsServer = new Server({port:8085}); 74 | wsServer.on("connection",websocket=>{ 75 | websocket.send("这个消息是服务器主动推送的"); 76 | websocket.on("message",message=>{ 77 | console.log("接收到message"+message); 78 | }) 79 | }) 80 | 81 | setInterval(()=>{ 82 | if(wsServer.clients){ 83 | wsServer.clients.forEach(client=>{ 84 | client.send("这是定时发送的") 85 | }) 86 | } 87 | },2000); -------------------------------------------------------------------------------- /.idea/workspace.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 74 | 75 | 76 | 78 | 79 | 88 | 89 | 90 | 91 | 92 | true 93 | DEFINITION_ORDER 94 | 95 | 96 | 97 | 98 | 99 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 |