Example of creating a simple chat room using nodejs + socket

Source: Internet
Author: User
Tags openssl require sendfile socket

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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.