All Products
Search
Document Center

Simple Log Service:Install Simple Log Service SDK for Node.js

Last Updated:Oct 26, 2023

Before you can use Simple Log Service SDK for Node.js to call the API operations of Simple Log Service, you must install Simple Log Service SDK for Node.js. This topic describes how to install Simple Log Service SDK for Node.js.

Prerequisites

  • Simple Log Service is activated. For more information, see Activate Simple Log Service.

  • An AccessKey pair is created and obtained. For more information, see AccessKey pair.

    An Alibaba Cloud account has permissions to call all API operations. If you use the AccessKey pair of an Alibaba Cloud account, security risks may occur. We recommend that you create and use a RAM user to call API operations or perform routine O&M. Make sure that the RAM user is granted the permissions to perform operations on Simple Log Service resources. For more information, see Grant permissions to a RAM user.

  • A Node.js development environment is set up. For more information, visit the official website of Node.js.

Precautions

Simple Log Service SDK for Node.js is developed based on JavaScript. TypeScript is not supported.

Installation

  1. Create a project directory and go to the directory.

  2. Run the following initialization command:

    npm init

    Complete the configurations as prompted. After the initialization is complete, a file named package.json is created. Example:

    {
      "name": "sls_node",
      "version": "1.0.0",
      "description": "",
      "main": "index.js",
      "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
      },
      "author": "",
      "license": "ISC"
    }
  3. Run the following command to install Simple Log Service SDK for Node.js:

    npm install aliyun-sdk

    After the command is run, the package.json file is injected with aliyun-sdk information. Example:

    {
      "name": "sls_node",
      "version": "1.0.0",
      "description": "",
      "main": "index.js",
      "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
      },
      "author": "",
      "license": "ISC",
      "dependencies": {
        "aliyun-sdk": "^1.12.3"
      }
    }
  4. Create a project. In the following example, Express is used to create a project.

    1. Run the following command to install Express:

      npm install express

      For more information, see Installing.

    2. Run the following command to install morgan:

      npm install morgan

      For more information, see morgan.

    3. Create a file named app.js and add the following code to the file:

      var express = require('express')
      var morgan = require('morgan')
      var app = express()
      const logger = morgan(function (tokens, req, res) {
        return [
          tokens.method(req, res),
          tokens.url(req, res),
          tokens.status(req, res),
          tokens.res(req, res, 'content-length'), '-',
          tokens['response-time'](req, res), 'ms'
        ].join(' ')
      })
      app.use(logger)
      app.get('/', (req, res) => res.send('Hello World!'))
      app.listen(3000, () => console.log('Example app listening on port 3000!'))
    4. Run the following command to start the project:

      node app.js

    After the project is created, the following result is returned:

    Example app listening on port 3000! 

FAQ

What do I do if the ../xx/jsSHA/src/sha.js file cannot be found?

The file of the dependent module is deleted. We recommend that you run the npm install aliyun-sdk command to reinstall the SDK. When you reinstall the SDK, the missing file is automatically retrieved.

What to do next

Get started with Log Service SDK for Node.js