1.0.0-beta.5 • Published 6 years ago

qwebs-http v1.0.0-beta.5

Weekly downloads
8
License
MIT
Repository
github
Last release
6 years ago

Qwebs

Web application framework with native promise, dependency-injection and bundle.

NPM Build Status Coverage Status NPM Download Dependencies Status

Discover our starter kit with Polymer.

Features

Installation

npm install $qwebs --save
npm install $qwebs-http --save

Create a service.js

"use strict";

class Service {
	constructor() {	
};

index(request, response) {
 let content = {
  text: `hello ${request.params.name}`
 };
 return response.send({ request: request, content: content });
};

exports = module.exports = Service;

Define services.json

{
    "services": [
        { "name": "$http", "location": "qwebs-http"},
        { "name": "$service", "location": "./service"}
    ]
}

Create config.json

{
    "services": "./services.json",
    "http": {
        "port": 3000
    }
}

Enjoy

Create a server.js

"use strict";

const Qwebs = require("qwebs");
new Qwebs().load();

Run server on http://localhost:3000

node server.js

Routing

Our goal is to find the final route as fast as possible. We use a tree data structure to represent all routes.

  • get(route, service, method)
  • post(route, service, method)
  • put(route, service, method)
  • patch(route, service, method)
  • delete(route, service, method)
{
    "services": [
        { "name": "$user", "location": "../services/info.es6"}
    ],
    "locators": [
        { "get": "/user/:id", "service": "$user", "method": "get" },
        { "post": "/user", "service": "$user", "method": "save" }
    ]
}
qwebs.get("/user/:id", "$users", "get"); 
qwebs.post("/user", "$users", "save");
...

Services

Qwebs is deigned for POO. Create service, define a route and attached them in routes.json. Qwebs has an dependency injector for easier integration.

class ApplicationService {
    //$config service is automatically injected
    constructor($config) {
        if ($config.verbose) console.log("ApplicationService created.");
    };

    //send javascript object
    get(request, response) {
        let content = { message: "Hello World" };   
        return response.send({ request: request, content: content });
    };

    //send stream
    stream(request, response, reject) {
        let stream = fs.createReadStream('file.txt')
                       .on("error", reject)           //reject Promise
                       .pipe(new ToUpperCase())       //transform
                       .on("error", reject)           //reject Promise
        return response.send({ request: request, stream: stream });
    };
};

exports = module.exports = ApplicationService;

Just declare the service name in your constructor.

class UserService {
    //Config service wil be created as a singleton and injected when UserService will be created
    constructor($config)

Qwebs will create your service with its dependencies.

{
    "services": [
        { "name": "$user", "location": "../services/user"}
        ...
//server.js
qwebs.inject("$user", "./services/user");

Http response are automatically extended to compressed with Gzip or Deflate.

  • response.send({request, statusCode, headers, content, stream})

You could override this default behaviour with POO. Override the default response service and inject the new one in Qwebs.

const ResponseService = require("qwebs/lib/services/response");

class MyResponseService extends ResponseService { constructor() { super(); };

send(response, dataToSend) {
    return new Promise((resolve, reject) => {
        if (dataToSend == undefined) reject(new Error("No data."));

        dataToSend.headers = data.headers || {};
        dataToSend.headers["Cache-Control"] = "private";
        dataToSend.headers["Expires"] = new Date(Date.now() + 3000).toUTCString();
        return super.send(response, dataToSend).then(resolve).catch(reject);
    });
};

};

exports = module.exports = MyResponseService;

Then replace $response service in $injector.

```routes.json
{
    "services": [
        { "name": "$response", "location": "../services/my-response"}
    ]
}
qwebs.inject("$response", "./services/my-response");

All assets are loaded in memory at startup. Uploaded images are not saved in temporary files. $qjimp service is designed to read, manipulate image stream.

You could create your own css or js bundle without WebPack. Qwebs includes a Sass preprocessor. You don't need to compile your sass via an external program.

{
    "/app.js":[
        "bower_components/angular-material/angular-material.js",
        "bower_components/angular-route/angular-route.js",
        "bower_components/angular-aria/angular-aria.js",
        "bower_components/angular-sanitize/angular-sanitize.js",
        "bower_components/angular-i18n/angular-locale_fr-fr.js",
        "bower_components/angular-animate/angular-animate.js",
        "web/app.js"
    ],
    "/app.css":[
        "assets/mixins.scss",
        "bower_components/angular-material/angular-material.css",
        "assets/master.scss"
    ]   
}
<!DOCTYPE html>
<html>
    <head>
        <link rel=stylesheet type="text/css" href="/app.css">
    </head>
    <body>
        <script src="/app.js"></script>
    </body>
</html>
  • Easier to read
  • Easier to maintain in the future
  • Easier error handling

Services

  • $config: your configuration.
  • $qwebs: qwebs instance.
  • $injector: resolve services at runtime.
  • $responseProxy: extand http.ServerResponse.
  • $response: default response extension.
  • $qjimp: convert and manipulate images.

Others Services

Examples

To run our examples, clone the Qwebs repo and install the dependencies.

$ git clone https://github.com/BenoitClaveau/qwebs --depth 1
$ cd qwebs
$ npm install
$ cd exemples/helloworld
$ node server.js

Test

To run our tests, clone the Qwebs repo and install the dependencies.

$ git clone https://github.com/BenoitClaveau/qwebs --depth 1
$ cd qwebs
$ npm install
$ cd tests
$ node.exe "../node_modules/mocha/bin/mocha" .
1.0.0-beta.51

6 years ago

1.0.0-beta.50

6 years ago

1.0.0-beta.49

6 years ago

1.0.0-beta.48

6 years ago

1.0.0-beta.47

6 years ago

1.0.0-beta.46

6 years ago

1.0.0-beta.45

6 years ago

1.0.0-beta.44

6 years ago

1.0.0-beta.43

6 years ago

1.0.0-beta.42

6 years ago

1.0.0-beta.41

6 years ago

1.0.0-beta.40

6 years ago

1.0.0-beta.39

6 years ago

1.0.0-beta.38

6 years ago

1.0.0-beta.37

6 years ago

1.0.0-beta.36

6 years ago

1.0.0-beta.35

6 years ago

1.0.0-beta.34

6 years ago

1.0.0-beta.33

6 years ago

1.0.0-beta.32

6 years ago

1.0.0-beta.31

6 years ago

1.0.0-beta.30

6 years ago

1.0.0-beta.29

6 years ago

1.0.0-beta.28

6 years ago

1.0.0-beta.27

6 years ago

1.0.0-beta.26

6 years ago

1.0.0-beta.25

6 years ago

1.0.0-beta.24

6 years ago

1.0.0-beta.23

6 years ago

1.0.0-beta.22

6 years ago

1.0.0-beta.21

6 years ago

1.0.0-beta.20

6 years ago

1.0.0-beta.19

6 years ago

1.0.0-beta.18

6 years ago

1.0.0-beta.17

6 years ago

1.0.0-beta.16

6 years ago

1.0.0-beta.15

6 years ago

1.0.0-beta.14

6 years ago

1.0.0-beta.13

6 years ago

1.0.0-beta.12

6 years ago

1.0.0-beta.11

6 years ago

1.0.0-beta.10

6 years ago

1.0.0-beta.9

6 years ago

1.0.0-beta.8

6 years ago

1.0.0-beta.7

6 years ago

1.0.0-beta.6

6 years ago

1.0.0-beta.5

6 years ago

1.0.0-beta.3

6 years ago

1.0.0-beta.2

6 years ago

1.0.0-beta.1

6 years ago

0.3.5

7 years ago

0.3.4

7 years ago

0.3.3

7 years ago

0.3.2

7 years ago

0.3.1

7 years ago

0.3.0

7 years ago

0.2.18

7 years ago

0.2.17

7 years ago

0.2.16

7 years ago

0.2.15

7 years ago

0.1.96

7 years ago

0.1.95

7 years ago

0.1.94

7 years ago

0.1.93

7 years ago

0.1.92

7 years ago

0.1.91

7 years ago

0.1.89

7 years ago

0.1.88

7 years ago

0.1.87

7 years ago

0.1.86

7 years ago

0.1.85

7 years ago

0.1.84

7 years ago

0.1.83

7 years ago

0.1.82

7 years ago

0.1.81

7 years ago

0.1.80

7 years ago

0.1.79

7 years ago

0.1.78

7 years ago

0.1.77

7 years ago

0.1.76

7 years ago

0.1.75

7 years ago

0.1.74

7 years ago

0.1.73

7 years ago

0.1.72

7 years ago

0.1.71

7 years ago

0.1.70

7 years ago

0.1.69

7 years ago

0.1.68

7 years ago

0.1.67

7 years ago

0.1.66

7 years ago

0.1.65

7 years ago

0.1.63

7 years ago

0.1.62

7 years ago

0.1.60

7 years ago

0.1.59

7 years ago

0.1.58

7 years ago

0.1.57

7 years ago

0.1.56

7 years ago

0.1.55

7 years ago

0.1.54

7 years ago

0.1.53

7 years ago