1.3.3 โ€ข Published 2 years ago

@hikyu/soap v1.3.3

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

Soap ๐Ÿงผ

Your new server ๐Ÿ’พ Simple, quick and light ๐Ÿค

npm.io npm.io npm.io

Install ๐Ÿ—ƒ

You may have to install a view engine of your choice too ๐Ÿ“Œ

npm i @hikyu/soap

Usage

I will use the simple ๐Ÿฃ require for demonstration

const soap =  require('@hikyu/soap');

// Run once if the server is started
soap.once('started', (data) => {
	console.log(`Server running on port ${data.port} (Listener)`);
});

// Start the server on port 3000 โš™
soap.init(3000);

Content

Routes

soap.get('path', callback);

soap.post('path', callback);

Smart-Routing

Automatically renders pages from the views folder. Depends on the requested path Example

// Enable smartRouting and set options
soap.smartRouting({
    // enabled: true,
    viewEngine: 'pug',
    defaultPage: 'landing' // localhost/ => ./views/landing.pug
});

// localhost/        => ./views/landing.pug
// localhost/page    => ./views/page.pug
// localhost/tasks/1 => ./views/tasks/1.pug

Render

It's pretty simple to use view engines ๐Ÿ‘€ in Soap ๐Ÿงผ Just add the extention to the path and render it! Avaiable view engines:

  • HTML
  • EJS
  • Pug
soap.get('/', (req, res) => {
	// HTML
	res.render('index.html');

	// EJS
	res.render('index.ejs', { siteName: "Soap ๐Ÿงผ" });

	// Pug
	res.render('index.pug', { siteName: "Soap ๐Ÿงผ" });
});

Send

Just want to show simple text ๐Ÿ“ƒ? So use send

soap.get('/', (req, res) => {
	res.send('Hey there, this is sent via Soap ๐Ÿงผ!');
});

JSON

Same with send there's JSON rendering aswell ๐Ÿ”ก

soap.get('/', (req, res) => {
	res.json([
		{ name: "Dog", emoji: "๐Ÿ•" },
		{ name: "Cat", emoji: "๐Ÿˆ" },
		{ name: "Llama", emoji: "๐Ÿฆ™" }
	]);
});

Parameters

Query

// Get query parameters is simple:
soap.get('/', (req, res) => {
	const query = req.query;
	res.send(`Your query: ${query}`);
});

Body

// For the body of post:
soap.post('/', async (req, res) => {
	// It's required to wait a bit
	const post = await req.post;
	res.send(`Your post body: ${post}`);
});

Folders

Views

Every file that will be rendered, should be in the ./views folder.

Public

Files that have to be user-reachable, should be in the ./public folder.

Events

// Run once if the server has started
soap.once('started', (data) => {
	console.log(`Server running on port ${data.port} (Listener)`);
});

// Run every time when a client requests a route
soap.on('request', (data) => {
	console.log(`Client requests on path ${data.path} (Listener)`);
});

// Run every time a file is sent to the client
soap.on('load', (data) => {
	console.log(`File ${data.path} loaded (Listener)`);
});

// Run every time a file is rendered
soap.on('render', (data) => {
	console.log(`File ${data.file} rendered on path ${data.path} (Listener)`);
});

// Run every time, json is sent
soap.on('json', (data) => {
	console.log(`Sent json data from ${data.path} (Listener)`);
});

// Run every time, pain text is sent
soap.on('send', (data) => {
	console.log(`Sent plaint text from ${data.path} (Listener)`);
});

// Run every time a path is not found
soap.on('404', (data) => {
	console.log(`Path ${data.path} not found (Listener)`);
});

// GET
soap.on('get', (data) => {
	console.log(`GET ${data.path}`);
});

// POST
soap.on('post', (data) => {
	console.log(`POST ${data.path}`);
});
1.3.3

2 years ago

1.3.2

2 years ago

1.3.1

2 years ago

1.2.2

2 years ago

1.2.1

2 years ago

1.1.7

2 years ago

1.1.6

2 years ago

1.1.5

2 years ago

1.1.4

2 years ago

1.1.3

2 years ago

1.1.2

2 years ago

1.1.1

2 years ago