GithubHelp home page GithubHelp logo

node's Introduction

Node.js

目录

配置说明:

系统

  • LSB Version: :core-4.1-amd64:core-4.1-noarch
  • Distributor ID: CentOS
  • Description: CentOS Linux release 7.2.1511 (Core)
  • Release: 7.2.1511
  • Codename: Core

node.js版本

  • v6.11.3

nmp版本

  • 3.10.10

测试代码示例

注意开放相关端口,可以用以下命令进行端口占用查询netstat -ntlp

http的测试代码

	const http = require('http');

	const hostname = '127.0.0.1';
	const port = 3000;

	const server = http.createServer((req, res) => {
		 res.statusCode = 200;
		 res.setHeader('Content-Type', 'text/plain');
		 res.end('Hello World\n');
	});

	server.listen(port, hostname, () => {
		 console.log(`Server running at http://${hostname}:${port}/`);
	});

https的测试代码

	// curl -k https://localhost:8000/
	const https = require('https');
	const fs = require('fs');

	const options = {
		 key: fs.readFileSync('test/fixtures/keys/agent2-key.pem'),
		 cert: fs.readFileSync('test/fixtures/keys/agent2-cert.pem')
	};

	https.createServer(options, (req, res) => {
		 res.writeHead(200);
		 res.end('hello world\n');
	}).listen(8000);

GET和POST请求(HTTPS模式)

GET请求

// curl -k https://localhost:8000/
var https = require('https');

const url = require('url');

const querystring = require('querystring');
var fs = require('fs');

var port='8000';

var hostname='127.0.0.1';

var options = {
  key: fs.readFileSync('test/fixtures/keys/agent2-key.pem'),
  cert: fs.readFileSync('test/fixtures/keys/agent2-cert.pem')
};

var getUrl = function(req, res){
	uri = req.url;
	if (uri !== '/favicon.ico') {
		console.log(uri);
		// 获取get参数字符串
		str = url.parse(uri).query;
		console.log(str);
		// 将参数字符串转换为json对象
		json = querystring.parse(str);
		console.log(json);
		res.write('GET node.js !');
		res.end();
	}
}

var server=https.createServer(options,getUrl);
server.listen(8000);

console.log(`Server running at http://${hostname}:${port}/`);

node's People

Contributors

fykang avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.