Introduction
A quick over of three ways to create a node.js application
Creating an Application Base
Method 1
Simply create your .js files and a launch.json file.
Run your scripts using node <file>.js
You might want to add a package.json file to ease the execution of your application. A basic package.json file looks like this
package.json
{ "name": "sample2", "version": "1.0.0", "description": "quick web demo", "main": "index.js", "scripts": { "test": "jest" }, "author": "Selvyn", "license": "ISC" }
Method 2
Use the npm init command. It will create the package.json file for you after prompting you to answer a few questions. You still need to create your basic .js files.
Method 3
Using the npm express-generator.
- Install express-generator using the command npm install -g express-generator
- Create an app using npm init
- Add express files to your project using the command npm install express --save
Adding files to the project
Add the following code to your default root file (normally index.js but could be any file you choose)
index.js
var express = require("express"); var app = express(); app.listen(3000, () => { console.log("Server running on port 3000"); });