nodejs-web-server

a simple web server with templating for nodejs

Usage no npm install needed!

<script type="module">
  import nodejsWebServer from 'https://cdn.skypack.dev/nodejs-web-server';
</script>

README

Nodejs-Web-Server

How to use

Nodejs-Web-Server is a wrapper around express.js, handlebars and jsonwebtoken.

Every app.listen method looks basically like this:

app.listen(Method, Path, callbacks)

To specify the Port add PORT to the .env file together with the port number.

PORT=Port_number

Methods

  • GET

  • POST

  • STATIC

  • API


GET

app.listen("GET", "/path/to/listen", (req, res) => {

});

callbacks:

  • req

  • res


POST

app.listen("POST", "/path/to/listen", (body, req, res) => {

});

callbacks:

  • body

  • req

  • res


STATIC

app.listen("STATIC", "/favicon.ico");

parameters

  • path to static file in static directory (root)

API

app.listen("API", "/api/v1/test", (decoded, body, req, res) => {
});

If the provided JWT-token is not valid, all callbacks will be null.

The JWT-secret is stored in an .env file under the tag SECRET:

SECRET=this_is_a_secret

callbacks:

  • decoded jwt token

  • body

  • req

  • res

Templating

nodejs-web-server also has a built-in templating engine built around handlebars. to use it, just type:

let  template = app.template("View Name", {
    data1:  "title",
    data2:  "logo"
});

I would recommend this in use together with the GET method.

app.listen("GET", "/path/to/listen", (req, res) => {
    res.send(app.template("view", {
        data1:  "title",
        data2:  "logo"
    }));
});

Required Directories

all directories have to be in the app's root path

  • static (only required if you use static function)

  • views (only required if you use the templating engine)

MySQL-Query

To use the built in MySQL-Driver use

app.db.query("SELECT NOW()", null, (err, res) => {
})

To use multiple SQL-Statements use

app.db.queryMultiple("CREATE DATABASE testtable; USE testtable; ...", null, (err, res) => {
})

For more information visit nodejs-mysql-driver.