Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

etmx

Package Overview
Dependencies
Maintainers
1
Versions
28
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

etmx

Typescript nodejs web server framework

  • 2.4.6
  • unpublished
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

ETMX Typescritp Server Framework

Typescript node.js 服务器开发框架

声明

本人英文不好,莫笑。

Install(安装)

npm install -g etmx

Use ETMX(使用ETMX)

etmx create [project name]

Description(描述)

ETMX is a typescript server framework, base on express framework.

ETMX contains:

1、 Server(use this class to create a server)

2、 Router(use this class to create a router)

3、 RouterManager(use this class to manage routers)

4、 Database(use this class to handle database)

Configurations(配置)

ETMX use a .json file or a object like to configure server, configurations contains:

  • server ------ server base configurations

    • port ------ port of web server listen, default is 3000

    • protocol ------ http or https, default is http

    • https ------ if https protocol used, configure this field

      • cert ------ ssl cert

      • key ------ private key of cert

    • path ------ this configurations contains used paths of server

    • cache ------ path of cache directory, default is /temp/etmx

    • static ------ path of static resources directory(s),

    • view ------ path of view, if you don't hav views, ignore it

    • cross ------ is cross-domain, default is false

    • favicon ------ path of favicon image

    • websocket ------ use web socket, default is false

    • log ------ is request log show, default is true

  • database ------ database configurations

    • type ------ database type, default is mysql, but now, only mysql can be used

    • host ------ database host, default is localhost

    • port ------ database port, this will use default port of database that you set

    • user ------ database user

    • password ------ password of database user

    • db ------ database to use

    • charset ------ charset of mysql to use, default is utf8

    • timezone ------ timezone of mysql to use, default is local

Create Route(创建路由)

use Router class to create a router, example:

import {Router} from 'etmx';

let router = new Router();

router
	//set router name
	.name('/main')
	//hello world
	.get('/test', (req, res, next) => {
		res.end('Hello world');
	})
	//mysql query
	.get('/user/:number/:name', (req, res, next) => {
		// use router.mysql.xxxx to handle mysql
		// use easy to create a mysql connection, this method can release connection automatically when connection unused.
		router.mysql.easy(async mysql => {
			try {
				let data = await mysql.query('select * from ??', 'user');
				res.json(data);
			} catch (e) {
				console.log(e);
			}
		});
	});
//must use export default to export a router
export default router;
Create server(创建服务器)

Now, let's create a server and start it

import {Server, RouterManager} from 'etmx';
import * as path from 'path';

//set path of server configurations
Server.confdir = path.join(__dirname, './');
//set server root file directory
Server.rootdir = __dirname;

//create a server use etmx.json file
let server = new Server('etmx');

//set routers
server.routers = app => {
	//create a router manager whit directory of current file
	let manager = new RouterManager(__dirname);
	//use manager.router = xxxx to add a router
	[
		'./Main',
	].forEach(_r => manager.router = _r);
	//manager.routers to get all routers
	return manager.routers;
};

//use create method to create a server
server.create();

How to use websocket (websocket使用)

Etmx provide a easy way to create and manage websocket, to use websocket you should do:

  • open serverconfig set websocket field a true value
  • create a websocet handler
  • open your webserver entry file and add websocket handler

:::notice:if your webserver use https websocket is wss://, if http websocket is ws://

webscket setting like:

{
	"server": {
		"websocket":true
	}
}

websocket handler like:

import {WSHandler, WSManager} from './';


//this is websocket params
interface Props {
	uid:string,		//give a uid param to mark a user
	uname:string,	//user name
}

export class HandlerName extends WSHandler<Props> {

	onConnect() {
		//when a socket connect to server
		console.log(this.params.uid);
		//send a welcome message
		this.send({type:'welcome', message:'Welcome to join this room'});
	}

	onClose() {
		//when a socket closed
		//send a user leave message
		WSManager.handlers.forEach(handler=>{
			if(handler.params.uid == this.params.uid) return;
			handler.send({type:'leave', message:`[${this.params.uname}] left this room`})
		})
	}

	onMessage(message) {
		//when receive message
		//send message to all but myself
		WSManager.handlers.forEach(handler=>{
			if(handler.params.uid == this.params.uid) return;
			handler.send({type:'text', message:message});
		})
	}

}

the end, in entry file set

server.wsHandler = HandlerName;

Keywords

FAQs

Package last updated on 15 May 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc