GithubHelp home page GithubHelp logo

simple-node's Introduction

How to start a new NPM project

  • Install node and npm. They are packaged together here.
  • Open a terminal window and find the directory you want to work in.
  • Run npm init in that folder and answer the questions. This will create a package.json file.
  • Create an index.js file. This is where we will start writing code!

You dont need a package.json file to run a node script. You can just create a script and run node script.js. However you need a package.json to install dependencies like third party libraries.

Sample project

Lets make a simple API server!

  • Install the third-party library "ExpressJS" which lets us run a node server.
  • Run npm install express. This will add Express to our package.json file and will include the library in the node_modules file.
  • Inside our index.js file include the following code:
const express = require('express')
const app = express()
const port = 3000

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

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})

Source

  • Modify the package.json's script block:
"scripts": {
  "start": "node index.js"
},
  • Run npm start. Alternatively you can just run node index.js, but using the package.json is preferable.
  • Visit http://localhost:3000/ and see it in action!

simple-node's People

Contributors

jimbol avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.