Skip to main content

Express

Make sure you have the express package installed in your project. If you don't have it installed, you can install it using the following command:

npm install express

How to use the Snippet

Write the snippet in your new file expsrv:

server.js
import express from 'express';

const app = express();

app.use(express.json());

const PORT = process.env.PORT || 3000;

app.get('/', (req, res) => {
res.send('Hello World!');
});

app.use((req, res) => {
res.status(404).send('404 Not Found');
});

app.listen(PORT, () => {
console.log('Server is running on port: ', PORT);
});

In the snippet above, we created a simple Express.js server that listens on port 3000. The server has two routes:

  1. The root route / that returns a Hello World! message.
  2. A 404 route that returns a 404 Not Found message for all other routes.

How to use the Command

  1. Open the command palette (Ctrl+Shift+P or Cmd+Shift+P on Mac).
  2. Type MERN: Express Function and press Enter.
  3. Select the function to create (Server, Router) and enter the file name.
  4. Press Enter and the snippet will be created.
Show GIF

Express Server


“You can make use of any with the right approach.” – Askeladd (Vinland Saga)