Creating a simple node.js template
Overview
This template simplifies the process of creating a basic Node.js project with the Express framework.
Template structure
Setup the template structure:
proplate init nodejs-templateFor more information about this command, see proplate init
Set up a node.js project
Let's set up a Node.js project similarly to how we typically would, to observe Proplate in action. The structure of the template is directly located within the generated nodejs-template directory
Initialize project
npm init -ySet up express.js
Install the necessary dependency
npm i expressTo expedite the template initialization process, we prefer to exclude the node_modules directory.
Users can install dependencies once the boilerplate is generated.
to inform proplate to ignore it during generation process, include node_modules, package-lock.json under the exclude section in meta.json
{
"id": "nodejs-template",
"args": [
{
"key": "description",
"label": "Describe your project",
"q_type": "Text"
}
],
"exclude": ["node_modules", "package-lock.json"]
}Create your server file
Create a new file, let’s call it server.mjs in the nodejs-template directory and add the following code:
import express from "express";
const app = express();
const port = 5000;
app.get("/", (req, res) => {
res.send("Welcome to my server!");
});
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});Try it
In the terminal:
node server.mjstry to bootstrap a project using our template
proplate create --template nodejs-template --dest new-projectFor more information about this command, see bootstrap template
Dynamic variables
Currently, having to manually change details such as the project name, description, etc..., upon project generation is not ideal. However, Proplate has a solution for this. It provides a way to define arguments and bind them to any file.
Let's set up dynamic values for the name and description by specifying them in meta.json.
{
"id": "nodejs-template",
"args": [
{
"key": "name",
"label": "Project name",
"q_type": "Text"
},
{
"key": "description",
"label": "Project description",
"q_type": "Text"
}
],
"exclude": ["node_modules", "package-lock.json"]
}For more information about dynamic variables, see dynamic variables
Let's add placeholders for them in package,
syntax is: $key
{
"name": "$name",
"description": "$description"
}proplate create --template nodejs-template --dest new-projectNow, Proplate prompts for these arguments interactively when bootstrapping a new project.