Server version NodeJS File / folder structure

I am creating my first project using serverless and while I find many great tutorials, I am having a hard time finding information on the actual structure of the project.

My thoughts are to use the below structure for my functions, shared libraries and kernel config / dependencies:

|- functions/
|--- users/
|----- handler.js
|----- serverless.yml
|--- roles/
|----- handler.js
|----- serverless.yml
|- shared/
|--- common.js
|- node_modules/
|- package.json

      

My main curiosity is about deployment and how it relates to dependencies and shared files. Also, automating the deployment of this framework seems odd since I'm going to need to deploy every one of the functions I can script, but wondering if this is necessary or advised.

+3


source to share


1 answer


I did a little bit with this and found it quite frustrating. If you deploy from your installation, what does your api look like? With separate serverless.yaml

files, you get independent api endpoints (assuming you are running api calls and not something like s3).

As a result, I got this structure:

|- serverless/
|--- serverlsss.yml
|--- web pack.config.js
|--- dist/
|--- node_modules() /* dev and common code */
|--- src/
|----- function1/
|-------- node_modules
|-------- package.json
|-------- index.js
|----- function2/
|-------- node_modules
|-------- package.json
|-------- index.js

      

I am using the webless serverlessless plugin to list individual functions in a directory dist/

. Then he serverless.yaml

points to it.

webpack.config.js

as follows:



const nodeExternals = require('webpack-node-externals');
const path = require('path');

module.exports = {
  entry: {
    'function1': './src/function1/index.js', 
    'function2': './src/function2/index.js',
  },
  target: 'node',
  output:{
    libraryTarget: 'commonjs2',
    path: path.join(__dirname, './dist'),
    filename: '[name].js'
  },
  externals: [nodeExternals()],
  module: {
    loaders: [
      /* Babel is nice, but it adding a some bulk and I don't need it
      {
        test: /\.js$/,
        loaders: ['babel'],
        include: __dirname,
        exclude: /node_modules/,
      }, */
      { 
        test: /\.json$/,
        loaders: ['json']} 
    ],
  },
};

//  externals: [nodeExternals()],
//  nodeExternals seems to break aws-sdk when serving locally
//  aws-sdk should be in dev-dependencies of root folder
//  that way it available in dev but doesn't get packaged.
//  It available to the lambdas when deployed.

      

After that, just make sure you set the individual flag to serverless.yml

package:
  individually: true

      

The webpack plugin is pretty nice and does most of the hard work. With this, I can do one deployment and all the functions turn out to be individual lambda functions under one api endpoint. You also get a Webpack dev server so you can run serverless webpack serve

to test your functions.

It was a bit painful to set up, but it worked really well.

+2


source







All Articles