Install the Node.js SDK
Before you can use the Node.js SDK to perform operations in Simple Log Service, you must install the SDK. This topic describes how to install the SDK.
Prerequisites
Simple Log Service is activated.
You have installed a Node.js development environment. For more information, see the official Node.js website.
Notes
The Simple Log Service Node.js SDK is developed in JavaScript and does not support TypeScript (TS).
Install the SDK
Create a project folder and navigate to the folder.
Run the following command to initialize the project.
npm initEnter
sls_nodefor the package name and accept the default values for the other parameters. After the initialization is complete, a package.json file is automatically created. The following code shows an example of the file content:{ "name": "sls_node", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", "license": "ISC" }Run the following command to install the Node.js SDK.
npm install @alicloud/logIf you encounter network issues when you use npm, you can use the npm mirror provided by Taobao.
After the command is run, information about @alicloud/log is added to the package.json file. The following code shows an example of the file content:
{ "name": "sls_node", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", "license": "ISC", "dependencies": { "@alicloud/log": "^1.2.5" } }Set up the project. This topic uses Express as an example.
Run the following command to install Express.
npm install expressFor more information, see Install Express.
Run the following command to install morgan.
npm install morganFor more information, see morgan.
Create an app.js file 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!'))Run the following command to start the project.
node app.js
The following result is returned.
Example app listening on port 3000!
FAQ
Cannot find the ../xx/jsSHA/src/sha.js file
This error occurs if a file from a dependent module is deleted. To resolve this issue, run the npm install aliyun-sdk command to reinstall the module. The missing file is automatically retrieved during the reinstallation.