class Greet {
/* No constructor required to set properties at instantiation */
#name = 'World';
get name() {
return this.#name;
}
set name(name) {
this.#name = name;
}
sayHello() {
console.log(`Hello, ${this.#name}`);
}
}
const {
Worker, isMainThread, parentPort, workerData
} = require('worker_threads');
if (isMainThread) {
module.exports = function parseJSAsync(script) {
return new Promise((resolve, reject) => {
const worker = new Worker(__filename, {
workerData: script
});
worker.on('message', resolve);
worker.on('error', reject);
worker.on('exit', (code) => {
if (code !== 0)
reject(new Error(`Worker stopped with exit code ${code}`));
});
});
};
} else {
const { parse } = require('some-js-parsing-library');
const script = workerData;
parentPort.postMessage(parse(script));
}
{
"scripts": {
"dev": "next",
"build": "next build",
"start": "next start"
}
}
Index.js
// pages/index.js
// Notice we do not need to import react or react-dom
export default () => {
return (<div>Hello JaxNode!</div>);
}
import axios from 'axios'
class weather extends React.Component {
static async getInitialProps() {
const results = await axios.get('https://someapi.com/restendpoint');
const data = results.data.mydata;
return { mydata: data };
}
render() {
return (<div>Your data is {this.props.mydata}.</div>)
}
}
export default weather;
import Link from 'next/link';
function Home() {
return (
<div>
Click{' '}
<Link href="/about">
<a>here</a>
</Link>{' '}
to read more
</div>
);
}
export default Home;
<Link as={`/p/${show.id}`}
href={`/post?id=${show.id}`}>
import Router from 'next/router';
function ReadMore() {
return (
<div>
Click <span onClick={() => Router.push('/about')}>here</span> to read more
</div>
);
}
export default ReadMore;
const express = require('express')
const next = require('next')
const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev })
const handle = app.getRequestHandler()
app
.prepare()
.then(() => {
const server = express()
server.get('/p/:id', (req, res) => {
const actualPage = '/post'
const queryParams = { title: req.params.id }
app.render(req, res, actualPage, queryParams)
})
server.get('*', (req, res) => {
return handle(req, res)
})
server.listen(3000, err => {
if (err) throw err
console.log('> Ready on http://localhost:3000')
})
})
.catch(ex => {
console.error(ex.stack)
process.exit(1)
})
function HelloWorld() {
return (
<div>
Hello world
<p>scoped!</p>
<style jsx>{`
p {
color: blue;
}
div {
background: red;
}
@media (max-width: 600px) {
div {
background: blue;
}
}
`}</style>
<style global jsx>{`
body {
background: black;
}
`}</style>
</div>
);
}
export default HelloWorld;