Note: Part of es6 is referenced. The chat room is simple, that is, the room creation, name change, simple chat, and other functions!
Command:
/Nick username // modify username
/Join room name // modify room name
Entry file server. js
Const http = require ("http ");
Const fs = require ("fs ");
Const path = require ("path ");
Const mime = require ("mime ");
Var cache = {};
Const hostname = "127.0.0.1 ";
Const port = 3000;
Var send404 = (response) => {
Response. writeHead (404, {"Content-Type": "text/plain "});
Response. write ("error: 404 ");
Response. end ()
}
Var sendFile = (response, filePath, fileContents) => {
Response. writeHead (200, {"Content-Type": mime. lookup (path. basename (filePath ))});
Response. end (fileContents)
}
Var serveStatic = (response, cache, absPath) => {
If (cache [absPath]) {
SendFile (response, absPath, cache [absPath]);
} Else {
Fs. exists (absPath, (exists) => {
If (exists ){
Fs. readFile (absPath, (err, data) => {
If (err ){
Send404 (response );
} Else {
Cache [absPath] = data;
SendFile (response, absPath, data)
}
})
} Else {
Send404 (response );
}
})
}
}
Var server = http. createServer (req, res) => {
Var filePath = false;
If (req. url = '/'){
FilePath = 'public/index.html'
} Else {
FilePath = 'public' + req. url
}
Var absPath = './' + filePath;
ServeStatic (res, cache, absPath)
});
Server. listen (port, hostname, () => {
Console. log ('Hello, welcome to nodejs, you are already at http: // $ {hostname }:$ {port }/')
});
Var chatServer = require ("./lib/chat_server ");
ChatServer. listen (server );
App. js demonstrates creating a local https server.
Var https = require ('https ')
Const fs = require ("fs ")
Const hostname = '2017. 0.0.1'
Const port = 3000
Const options = {
Key: fs. readFileSync ("./key. pem "),
Cert: fs. readFileSync ("./key-cert.pem ")
}
Const server = https. createServer (options, (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 https: // $ {hostname }:: {port }/')
})
Create the private key. pem command for the local https server
Openssl genrsa 1024> key. pem command
In addition to the private key, you also need to use the certificate to create a local certificate command:
Openssl req-x509-new-key. pem> key-cert.pem
The basic code is as follows:
Var https = require ('https ')
Const fs = require ("fs ")
Const hostname = '2017. 0.0.1'
Const port = 3000
Const options = {
Key: fs. readFileSync ("./key. pem "),
Cert: fs. readFileSync ("./key-cert.pem ")
}
Const server = https. createServer (options, (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 https: // $ {hostname }:: {port }/')
})
Because our certificates are not issued by the certificate authority, there will be a security prompt. If you want to use https on the server, you need to find a certificate authority (CA) to register, and obtain a real and trusted certificate for your server!