Security News
NVD Backlog Tops 20,000 CVEs Awaiting Analysis as NIST Prepares System Updates
NVD’s backlog surpasses 20,000 CVEs as analysis slows and NIST announces new system updates to address ongoing delays.
express-engine-jsx
Advanced tools
Example of users.jsx
view file
var Layout = require('./layout');
<Layout>
<ul class="users">
{users.map(user => (
<li key={user}>{user.name}</li>
))}
</ul>
</Layout>
Example of layout.jsx
view file
<html>
<head>
<meta charset="UTF-8"/>
</head>
<body>{children}</body>
</html>
Example of router
app.get('/users', function (req, res) {
res.render('users', {
users: [
{name: 'Max'},
{name: 'Bob'}
]
});
});
Output html
<!DOCTYPE html>
<html>
<head><meta charset="UTF-8"/></head>
<body><ul class="users"><li>Max</li><li>Bob</li></ul></body>
</html>
When you render some view, this engine takes jsx
file like this
var Layout = require('./layout');
<Layout>
<ul class="users">
{users.map(user => (
<li key={user}>{user.name}</li>
))}
</ul>
</Layout>
and compiles it to js
file like this
var React = require('react');
var requireJSX = require('express-engine-jsx/require');
module.exports = function (props) {
var __components = [];
with (props) {
var Layout = requireJSX('./layout');
__components.push(
React.createElement(
Layout,
null,
React.createElement(
'ul',
{className: 'users'},
users.map(user => (
React.createElement(
'li',
{key: user},
user.name
)
))
)
)
);
}
return __components;
};
and now this component can be rendered to html with ReactDOM.renderToStaticMarkup()
.
As you can see, each jsx view file returns array of components and standard html attributes are converted to react attributes
<div class="first" tabindex="1"></div>
<div class="second" tabindex="2"></div>
//...
__components.push(React.createElement('div', {className: 'first', tabIndex: '1'}));
__components.push(React.createElement('div', {className: 'second', tabIndex: '2'}));
//...
return __components;
var express = require('express');
var app = express();
require('express-engine-jsx').attachTo(app, {
cache: __dirname + '/cache', // required and should be absolute path to cache dir for compiled js files
views: __dirname + '/views', // required and should be absolute path to views dir with jsx files
doctype: '<!DOCTYPE html>' // optional and this is default value
});
That's it, you no need to do app.set('views', 'views')
and so on, attachTo
will do that for you
var engine = require('express-engine-jsx');
It's a function which takes three arguments:
path
- path to jsx filelocals
- object with properties which will be local variables in jsx filecallback
- Node style callback which will receive html string as second argumentAlso it has method attachTo
which takes two arguments:
server
- Express instanceoptions
- object which will be merged to optionsvar options = require('express-engine-jsx/options');
Object which has three properties:
cache
- absolute path to cache directoryviews
- absolute path to views directorydoctype
- string which will be prepended to output html, default value is "<!DOCTYPE html>\n"
This options used by require
var requireJSX = require('express-engine-jsx/require');
This is a function which you can use as regular require
but this one can run jsx files. It checks if path is jsx file and if it is then requireJSX
will convert this file to js file and put in cache dir and then run it.
var convert = require('express-engine-jsx/convert');
It is a function which can convert jsx view files to js files. It takes only two arguments:
jsxPath
- path to jsx filejsPath
- path where js file should be savedBest way is to watch jsx files with you favorite tool like gulp or grunt and use convert to update cached files.
MIT, see LICENSE
file
FAQs
JSX engine for ExpressJS
The npm package express-engine-jsx receives a total of 25 weekly downloads. As such, express-engine-jsx popularity was classified as not popular.
We found that express-engine-jsx demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
NVD’s backlog surpasses 20,000 CVEs as analysis slows and NIST announces new system updates to address ongoing delays.
Security News
Research
A malicious npm package disguised as a WhatsApp client is exploiting authentication flows with a remote kill switch to exfiltrate data and destroy files.
Security News
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.