Quickstart
Let’s write a simple HTTP server using the built-in JSTime.serve API. First, create a fresh directory.
$ mkdir quickstart$ cd quickstartRun jstime init to scaffold a new project. It’s an interactive tool; for this tutorial, just press enter to accept the default answer for each prompt.
$ jstime initjstime init helps you get started with a minimal project and tries toguess sensible defaults. Press ^C anytime to quit.
package name (quickstart):entry point (index.ts):
Done! A package.json file was saved in the current directory. + index.ts + .gitignore + tsconfig.json (for editor auto-complete) + README.md
To get started, run: jstime run index.tsSince our entry point is a *.ts file, JSTime generates a tsconfig.json for you. If you’re using plain JavaScript, it will generate a jsconfig.json instead.
Run a file
Section titled “Run a file”Open index.ts and paste the following code snippet, which implements a simple HTTP server with JSTime.serve.
const server = JSTime.serve({ port: 3000, fetch(req) { return new Response(`JSTime!`); },});
console.log(`Listening on http://localhost:${server.port}...`);Run the file from your shell.
$ jstime index.tsListening at http://localhost:3000...Visit http://localhost:3000 to test the server. You should see a simple page that says “JSTime!”.
Run a script
Section titled “Run a script”JSTime can also execute "scripts" from your package.json. Add the following script:
{ "name": "quickstart", "module": "index.ts", "type": "module",+ "scripts": {+ "start": "jstime run index.ts"+ }, "devDependencies": { "jstime-types": "^0.7.0" } }Then run it with jstime run start.
$ jstime run start $ jstime run index.ts Listening on http://localhost:4000...⚡️ Performance — jstime run is roughly 28x faster than npm run (6ms vs 170ms of overhead).
Install a package
Section titled “Install a package”Let’s make our server a little more interesting by installing a package. First install the figlet package and its type declarations. Figlet is a utility for converting strings into ASCII art.
$ jspm add figlet$ jspm add -d @types/figlet # TypeScript users onlyUpdate index.ts to use figlet in the fetch handler.
+ import figlet from "figlet";
const server = JSTime.serve({ fetch() {+ const body = figlet.textSync('JSTime!');+ return new Response(body);- return new Response(`JSTime!`); }, port: 3000, });Restart the server and refresh the page. You should see a new ASCII art banner.
____ _ | __ ) _ _ _ __ | | | _ \| | | | '_ \| | | |_) | |_| | | | |_| |____/ \__,_|_| |_(_)