{"id":13400828,"url":"https://github.com/reactjs/express-react-views","last_synced_at":"2025-09-30T05:31:23.062Z","repository":{"id":15573398,"uuid":"18308864","full_name":"reactjs/express-react-views","owner":"reactjs","description":"This is an Express view engine which renders React components on server. It renders static markup and *does not* support mounting those views on the client.","archived":true,"fork":false,"pushed_at":"2022-02-01T06:50:33.000Z","size":451,"stargazers_count":2734,"open_issues_count":27,"forks_count":262,"subscribers_count":67,"default_branch":"master","last_synced_at":"2024-10-29T21:51:54.682Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/reactjs.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-03-31T21:35:14.000Z","updated_at":"2024-10-29T12:07:10.000Z","dependencies_parsed_at":"2022-08-29T11:30:37.447Z","dependency_job_id":null,"html_url":"https://github.com/reactjs/express-react-views","commit_stats":null,"previous_names":[],"tags_count":21,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/reactjs%2Fexpress-react-views","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/reactjs%2Fexpress-react-views/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/reactjs%2Fexpress-react-views/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/reactjs%2Fexpress-react-views/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/reactjs","download_url":"https://codeload.github.com/reactjs/express-react-views/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":234707372,"owners_count":18874671,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-30T19:00:55.929Z","updated_at":"2025-09-30T05:31:17.772Z","avatar_url":"https://github.com/reactjs.png","language":"JavaScript","readme":"# express-react-views\n\nThis is an [Express][express] view engine which renders [React][react] components on server. It renders static markup and *does not* support mounting those views on the client.\n\nThis is intended to be used as a replacement for existing server-side view solutions, like [jade][jade], [ejs][ejs], or [handlebars][hbs].\n\n\u003chr\u003e\n\n## Deprecated\n\nThis package is no longer maintained. There are better options, though not all work with Express. I recommend [Next.js](https://nextjs.org/) or [Remix](https://remix.run/).\n\n\u003chr\u003e\n\n\n## Usage\n\n```sh\nnpm install express-react-views react react-dom\n```\n\n**Note:** You must explicitly install `react` as a dependency. Starting in v0.5, `react` is a peer dependency here. This is to avoid issues that may come when using incompatible versions.\n\n### Add it to your app.\n\n```js\n// app.js\n\nvar app = express();\n\napp.set('views', __dirname + '/views');\napp.set('view engine', 'jsx');\napp.engine('jsx', require('express-react-views').createEngine());\n```\n\n### Options\n\nBeginning with v0.2, you can now pass options in when creating your engine.\n\noption | values | default\n-------|--------|--------\n`doctype` | any string that can be used as [a doctype](http://en.wikipedia.org/wiki/Document_type_declaration), this will be prepended to your document | `\"\u003c!DOCTYPE html\u003e\"`\n`beautify` | `true`: beautify markup before outputting (note, this can affect rendering due to additional whitespace) | `false`\n`transformViews` | `true`: use `babel` to apply JSX, ESNext transforms to views.\u003cbr\u003e**Note:** if already using `babel-register` in your project, you should set this to `false` | `true`\n`babel` | any object containing valid Babel options\u003cbr\u003e**Note:** does not merge with defaults | `{presets: ['@babel/preset-react', [ '@babel/preset-env', {'targets': {'node': 'current'}}]]}`\n\nThe defaults are sane, but just in case you want to change something, here's how it would look:\n\n```js\nvar options = { beautify: true };\napp.engine('jsx', require('express-react-views').createEngine(options));\n```\n\n\n### Views\n\nUnder the hood, [Babel][babel] is used to compile your views to code compatible with your current node version, using the [react][babel-preset-react] and [env][babel-preset-env] presets by default. Only the files in your `views` directory (i.e. `app.set('views', __dirname + '/views')`) will be compiled.\n\nYour views should be node modules that export a React component. Let's assume you have this file in `views/index.jsx`:\n\n```js\nvar React = require('react');\n\nfunction HelloMessage(props) {\n  return \u003cdiv\u003eHello {props.name}\u003c/div\u003e;\n}\n\nmodule.exports = HelloMessage;\n```\n\n### Routes\n\nYour routes would look identical to the default routes Express gives you out of the box.\n\n```js\n// app.js\n\napp.get('/', require('./routes').index);\n```\n\n```js\n// routes/index.js\n\nexports.index = function(req, res){\n  res.render('index', { name: 'John' });\n};\n```\n\n**That's it!** Layouts follow really naturally from the idea of composition.\n\n### Layouts\n\nSimply pass the relevant props to a layout component.\n\n`views/layouts/default.jsx`:\n```js\nvar React = require('react');\n\nfunction DefaultLayout(props) {\n  return (\n    \u003chtml\u003e\n      \u003chead\u003e\u003ctitle\u003e{props.title}\u003c/title\u003e\u003c/head\u003e\n      \u003cbody\u003e{props.children}\u003c/body\u003e\n    \u003c/html\u003e\n  );\n}\n\nmodule.exports = DefaultLayout;\n```\n\n`views/index.jsx`:\n```js\nvar React = require('react');\nvar DefaultLayout = require('./layouts/default');\n\nfunction HelloMessage(props) {\n  return (\n    \u003cDefaultLayout title={props.title}\u003e\n      \u003cdiv\u003eHello {props.name}\u003c/div\u003e\n    \u003c/DefaultLayout\u003e\n  );\n}\n\nmodule.exports = HelloMessage;\n```\n\n\n## Questions\n\n### What about partials \u0026 includes?\n\nThese ideas don't really apply. But since they are familiar ideas to people coming from more traditional \"templating\" solutions, let's address it. Most of these can be solved by packaging up another component that encapsulates that piece of functionality.\n\n### What about view helpers?\n\nI know you're used to registering helpers with your view helper (`hbs.registerHelper('something', ...))`) and operating on strings. But you don't need to do that here.\n\n* Many helpers can be turned into components. Then you can just require and use them in your view.\n* You have access to everything else in JS. If you want to do some date formatting, you can `require('moment')` and use directly in your view. You can bundle up other helpers as you please.\n\n### Where does my data come from?\n\nAll \"locals\" are exposed to your view in `this.props`. These should work identically to other view engines, with the exception of how they are exposed. Using `this.props` follows the pattern of passing data into a React component, which is why we do it that way. Remember, as with other engines, rendering is synchronous. If you have database access or other async operations, they should be done in your routes.\n\n\n## Caveats\n\n* I'm saying it again to avoid confusion: this does not do anything with React in the browser. This is *only* a solution for server-side rendering.\n* This currently uses `require` to access your views. This means that contents are cached for the lifetime of the server process. You need to restart your server when making changes to your views. **In development, we clear your view files from the cache so you can simply refresh your browser to see changes.**\n* React \u0026 JSX have their own rendering caveats. For example, inline `\u003cscript\u003e`s and `\u003cstyle\u003e`s will need to use `dangerouslySetInnerHTML={{__html: 'script content'}}`. You can take advantage of ES6 template strings here.\n\n```js\n\u003cscript dangerouslySetInnerHTML={{__html: `\n  // Google Analytics\n  // is a common use\n`}} /\u003e\n```\n\n* It's not possible to specify a doctype in JSX. You can override the default HTML5 doctype in the options.\n\n## Contributors\n* [Paul O’Shannessy][zpao] (Author)\n* [Venkat Reddy][svenkatreddy]\n\n[express]: http://expressjs.com/\n[react]: https://reactjs.org/\n[jade]: http://jade-lang.com/\n[ejs]: https://ejs.co/\n[hbs]: https://github.com/pillarjs/hbs\n[babel]: https://babeljs.io/\n[babel-preset-react]: https://babeljs.io/docs/plugins/preset-react/\n[babel-preset-env]: https://babeljs.io/docs/plugins/preset-env/\n[zpao]: https://github.com/zpao\n[svenkatreddy]: https://github.com/svenkatreddy\n","funding_links":[],"categories":["Uncategorized","JavaScript","Awesome React","例子","React [🔝](#readme)"],"sub_categories":["Uncategorized","Tools"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Freactjs%2Fexpress-react-views","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Freactjs%2Fexpress-react-views","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Freactjs%2Fexpress-react-views/lists"}