Pages

Monday, 28 October 2024

Pajamas (PJS) - An easy way to write node.js apps.


npm version Dependencies

Installation

npm install -g pjs-cli

Usage

pjs

By default, pjs will listen on the port 8080 (or process.env.PORT) and read the current folder. You can change this settings in http://localhost/_/, take a look at the usage documentation.

Inside the folder where pjs is reading you can prototype any node.js applications thanks to the .pjs files (see examples below).

For using pjs as a daemon, your can use these commands:

  • pjs start (start pjs as a daemon)
  • pjs stop
  • pjs restart
  • pjs status

Examples

To launch the examples on your computer, install first pjs-cli and then :

git clone https://github.com/Atinux/pjs.git
cd pjs/examples/
pjs

You can visit http://localhost:8080 too run all the examples presented below.

What are these .pjs files ?

It's 90% of EJS templates with a done() method for making every asynchronous operations possible! (take a look at the examples #2)

Example 1 - Basis

Let's say that I have a file name hello.pjs in my current folder:

<% var foo = 'PJS'; %>
Hello <%= foo %>!

I launch the cli in my current folder: pjs

Then, I visit : http://localhost:8080/hello.pjs

Result: Hello PJS!

Example 2 - Asynchronous

What about if we want something a little bit more asynchronous?

File async.pjs:

<%
var request = require('request'),
    name;
request.get('http://www.mocky.io/v2/56af5cec1100004516f9bc90', function (err, res, body) {
  // body = { "name": "PJS" }
  name = JSON.parse(body).name;
  done();
});
%>
Hello <%= name %>!

As you can see, I can require a module, for that, please make sure to run npm install request in the current directory before visiting http://localhost:8080/async.pjs

The result will be : Hello PJS!

Notice the done(); method, it is important here for PJS to wait until the request is completed and the name set before going further in the template.

Example 3 - Includes

You may also want to include other .pjs files, that's why you can use the <% include your_file.pjs %> directly in your templates.

File include.pjs:

<p>I'm including hello.pjs</p>
<%- include hello.pjs %>

Result on http://localhost:8080/include.pjs

I'm including hello.pjs
Hello bar!

Actually, only this pre-processor include works. <%- include(file, { ... }); %> is not available yet.

Example 4 - REQUEST

Inside each .pjs file, you have access of the REQUEST variable. It contains some of the properties listed from Express (http://expressjs.com/en/4x/api.html#req);

File: request.pjs

<% if (REQUEST.method === 'POST') { %>
  <p><b>New todo:</b> <%= REQUEST.body.todo %></p>
<% } %>
<form method="post">
  <input type="text" name="todo" placeholder="Learn Piano..." />
  <button type="submit">Add todo</button>
</form>

If you fill the input and click on "Add todo", the condition will pass and "New todo:" will be displayed on the screen with the content of the input.

List of the properties available in REQUEST:

- baseUrl
- body
- headers
- hostname
- fresh
- ip
- ips
- method
- originalUrl
- path
- protocol
- query
- secure
- stale
- subdomains
- url
- xhr

Aliases

No comments:

Post a Comment