JavaScript:
Starting a new project
How to:
Before you write code, decide on tools and structure. Let’s use Node.js and npm (Node Package Manager) for this example.
- Install Node.js from the official website.
- Open a terminal and run:
npm init
Answer the setup questions. Boom—package.json
is created, describing your project. Next, let’s add Express, a popular web framework:
npm install express --save
Now, write a simple web server in index.js
:
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('Hello World!');
});
app.listen(3000, () => {
console.log('Server is up on port 3000');
});
Run your server:
node index.js
Sample output:
Server is up on port 3000
Navigate to http://localhost:3000
in your web browser. You should see “Hello World!”.
Deep Dive
Historically, project setup was a pain, with lots of manual configuration. Nowadays, tools like npm do the heavy lifting. For front-end projects, consider create-react-app
or vue-cli
. For Node.js, Express is a solid choice, balancing power with simplicity. It’s lightweight but has robust features for most web server needs.
Remember, how you organize your project is critical. Traditional Node.js apps have an entry point (like index.js
), a package.json
file to manage dependencies, and a folder structure that separates concerns (modules, utilities, routes, etc.).
Alternatives to npm for package management include Yarn, which offers speed and consistency improvements. For project scaffolding, Yeoman helps by providing generators for many types of projects and technologies.
See Also
- Node.js docs
- Express official site
create-react-app
GitHub repo- Vue CLI docs
- Yarn official site
- Yeoman official site